Forum Discussion

SathishF5Dasam's avatar
SathishF5Dasam
Icon for Nimbostratus rankNimbostratus
Jun 04, 2016

Regex to irule

Hi All,

 

I'm new to Irule and find difficult to migrate this regex into irule

 

Regex RewriteRule ^/company/(.*)(?http://www.test.com/company/$1/en_gb [R=301,L]

 

My config

 

"/company/*" { HTTP::redirect "http://www.test.com[HTTP::uri]en_gb"

 

Condition: if Uri matched "/company/*"(wildcard) then the suffix "/en_gb" should added for all company starting uri

 

Ex: /company/auto ---> company/auto/en_gb

 

/company/bike ---> company/bike/en_gb

 

many webpages have same condition looking,

 

appreciate irule coders.

 

  • Hi,

    the irule below is a basics for your need :

    when HTTP_REQUEST {
        if { [HTTP::path] starts_with "/company" and !([HTTP::path] contains "en_gb") } {
            set uri "http://www.test.com[HTTP::path]/en_gb"
            HTTP::respond 301 noserver "Location" "$uri" Cache-Control" "no-cache, must-revalidate"
        }
    }
    

    Hope this help

  • Here the same irule a bit modified to fit for all webpages you need to rewrite :

    For this one, you will need to create a string datagroup first named "rewrite_uri" and add all your starts uri (/auto, /bike, etc.)

    when HTTP_REQUEST {
        if { [class match [HTTP::path] starts_with rewrite_uri] and !([HTTP::path] contains "en_gb") } {
            set uri "http://www.test.com[HTTP::path]/en_gb"
            HTTP::respond 301 noserver "Location" "$uri" Cache-Control" "no-cache, must-revalidate"
        }
    }
    
  • And this one take into account Query string that may be present in the initial request :

    when HTTP_REQUEST {
        if { [class match [HTTP::path] starts_with rewrite_uri] and !([HTTP::path] contains "en_gb") } {
            if { [HTTP::query] eq "" } {
                set uri "http://www.test.com[HTTP::path]/en_gb"
            } else {
                set uri "http://www.test.com[HTTP::path]/en_gb?[HTTP::query]"
            }
            HTTP::respond 301 noserver "Location" "$uri" Cache-Control" "no-cache, must-revalidate"
        }
    }