Forum Discussion

Thierry_John's avatar
Thierry_John
Icon for Nimbostratus rankNimbostratus
Jun 26, 2019

Convert Microsoft rewrite rules to iRule

Hello,

I would like to convert Microsoft rewrite rules to iRule :

  <rewrite>

     <rules>

    <rule name="rule1" stopProcessing="true">

    <match url=".*" ignoreCase="true" />

<conditions trackAllCaptures="true" logicalGrouping="MatchAll">

        <add input="{HTTP_USER_AGENT}" pattern="(google|bot|spider|pinterest|crawler|archiver|flipboardproxy|mediapartners|facebookexternalhit|insights|quora|whatsapp|slurp)" />

        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

     </conditions>

     <action type="Rewrite" url="https://www.domain.com{REQUEST_URI}" appendQueryString="false"/>

    </rule>

    <rule name="rule2" stopProcessing="true">

     <match url=".*" ignoreCase="true" />

     <conditions trackAllCaptures="true" logicalGrouping="MatchAll">

        <add input="{HTTP_FROM}" pattern=".+" />

        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

     </conditions>

     <action type="Rewrite" url="https://www.domain.com{REQUEST_URI}" appendQueryString="false"/>

    </rule>

     </rules>

  </rewrite>

 

Regards,

Thierry

3 Replies

  • Thierry,

    I'll admit I'm no Microsoft expert so here's my best attempt so far for rule1:

    # <rule name="rule1" stopProcessing="true"> 
    #    <match url=".*" ignoreCase="true" />
    #    <conditions trackAllCaptures="true" logicalGrouping="MatchAll">
    #       <add input="{HTTP_USER_AGENT}" pattern="(google|bot|spider|pinterest|crawler|archiver|flipboardproxy|mediapartners|facebookexternalhit|insights|quora|whatsapp|slurp)" />
    #       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    #    </conditions>
    #    <action type="Rewrite" url="https://www.domain.com{REQUEST_URI}" appendQueryString="false"/>
    # </rule>
    # 
     
    when HTTP_REQUEST {
    	if {[matchclass [HTTP::header "User-Agent"] equals bot_datagroup] && ![[HTTP::uri] contains "."]}{
    		HTTP::respond 301 Location https://www.domain.com[HTTP::uri]
    	}	
    }

    This rule requires you create a datagroup on the F5 titled 'bot_datagroup', the rule does a string match to see if the User-Agent header matches any of the entries listed in the 'bot_datagroup' datagroup and checks if the URI does not contain a dot (.) as some means to see if it's a file being referenced.

    The bot datagroup would contain the following strings: google, bot, spider, pinterest, crawler, archiver, flipboardproxy, mediapartners, facebookexternalhit, insights, quora, whatsapp, slurp.

    If you can clarify a bit what your expectation is for the F5 I could probably clean the rule up significantly.

  • Thank you for this answer.

    But I don't want to redirect to specified URL, I want to rewrite URL.