Forum Discussion

networkdawg_303's avatar
networkdawg_303
Icon for Nimbostratus rankNimbostratus
Dec 14, 2016

iRules - Remove WWW AND replace "Host"

Hello,

 

I am sorry if this is very easy to do, but I am really new to F5 and iRules.

 

We have an iRule in place which replaces the domain part of a URL. This rule is below.

 

The situation is this: If a user visits location1.domain1.com , it translates to location1.primarydomain.com, and then our webserver handles redirection to .

 

That part is working great. HOWEVER, if a user appends www in front of the URL, i.e. , the URL is then , and our webserver doesn't know what do to.

 

I would like the iRule to remove www, and then translate it to location1.primarydomain.com (if you know what I mean).

 

Is this possible?

 

when HTTP_REQUEST { if { [HTTP::host] ends_with ".domain1.com" } { HTTP::header replace "Host" [string map {".domain1.com" ".primarydomain.com"} [HTTP::host]] } }

 

2 Replies

  • You need this logic inserted into your existing iRule:

    if { [HTTP::host] starts_with "www." } {
    HTTP::host [getfield [HTTP::host] "www." 2]
    }
    

    or you can try this if the above doesn't give the right result:

    if { [HTTP::host] starts_with "www." } {
    HTTP::host [string range [HTTP::host] 4 end]
    }
    
  • Hi networkdawg,

    you may try the iRule below.

    It uses the

    [domain]
    command to extract the first three dotted domain elements (e.g. four.third.second.frist) from the requested HOST-header, then passes the information to a
    [getfield]
    command to extract the third element and adds the new second and first dotted domain elements via TCL concentation...

    when HTTP_REQUEST { 
        if { [string tolower [HTTP::host]] ends_with ".domain1.com" } then { 
            HTTP::host "[getfield [domain [HTTP::host] 3] "." 1].primarydomain.com"  
        } 
    }
    

    Cheers, Kai