Forum Discussion

bd94_306725's avatar
bd94_306725
Icon for Altostratus rankAltostratus
Oct 06, 2017

Irule Regular expression for HTTP redirect

Hello,

i've got a lot of domain like country.mydomain.com

When users write , they are redirect to fr.mydomain.com. To do that i'm using this iRule :

when HTTP_REQUEST { 
    set host [string tolower [HTTP::host]] 
    set uri [HTTP::uri] 

    if {[class match -- $host equals my_datagroup_list]} { 
        HTTP::respond 301 noserver Location "https://us.mydomain.com${uri}" 
        } 

    //elseif ... 

    else { 
       switch [string tolower [HTTP::host]] { 

         "www.mydomain.co.at" { HTTP::respond 301 noserver Location "https://at.mydomain.com${uri}" } 
         ... 
         ... 

         and hundred of line for each country... that is not really good.. 

I try to do this with this regex :

What I want : if user write : --> redirect to --> fr.mydomain.com (delete www.) and --> redirect to --> fr.mydomain.com

Actually i've got something like this, but still not working with regex.. can you help me ? 🙂

elseif {${uri} matches_regex {^www\.\\w+\.mydomain\.com} } { 
        set regexp_result [regexp {view=(\\w+)} $uri viewstring countryDomain] 
        HTTP::respond 301 noserver Location "https://countryDomain.mydomain.com" 
    }

Should I replace \w by [a-zA-Z] ?

And i'm not really sur about this line :

set regexp_result [regexp {view=(\\w+)} $uri viewstring countryDomain] 

Thanks for your time,

Hugo

1 Reply

  • you can try something like that

    switch -glob -- [set host [HTTP::host]] {
        "www.*.domain.com" { HTTP::respond 301 noserver Location "https://[getfield $host "." 2].domain.com${uri}"}
        "www.domain.co.*" { HTTP::respond 301 noserver Location "https://[getfield $host "." 4].domain.com${uri}"}
        "www.domain.*" { HTTP::respond 301 noserver Location "https://[getfield $host "." 3].domain.com${uri}"}
    }
    

    Note: Edited to correct getfield command