Forum Discussion

Jeff_Unger_1067's avatar
Jeff_Unger_1067
Icon for Nimbostratus rankNimbostratus
Dec 21, 2005

Strip leading www

We have some special needs customers who do not realize that you can reach a website without adding a leading "www" to the URL. So, in order to accomodate those folks, we have aliases setup in DNS. For example: www.excel.gosolo.com is just a CNAME record that points to excel.gosolo.com.

 

 

My goal is to strip the leading "www" from the HTTP header whenever it is encountered for the clients I mentioned. I know this can be done using a redirect irule, which is what I am doing now.

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] equals "www.excel.gosolo.com" } {

 

HTTP::redirect "http://excel.gosolo.com"

 

}

 

}

 

 

But can it be done using a replace instead? There is no need to do a redirect since it will go to the same IP address, and it actually is causing problems with another part of our site, so I tried the following irule:

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] equals "www.excel.gosolo.com" } {

 

HTTP::header replace "excel.gosolo.com"

 

}

 

}

 

 

That did not work. Any ideas?

 

 

Thanks,

 

  • You'll need to specify the header name in your assignment.

     

     

    HTTP::header replace []

     

     

    Replaces the last occurrence of the named header with the string . This command performs a header insertion if the header was not present.

     

     

    So, your code should look like this:

     

     

    when HTTP_REQUEST {
      if { [HTTP::host] equals "www.excel.gosolo.com" } {
        HTTP::header replace "Host" "excel.gosolo.com"
      }
    }

     

     

    Another option you could go with is a more generic approach if you have more than one domain to deal with.

     

     

     when HTTP_REQUEST {
      if { [HTTP::host] starts_with "www." } {
        HTTP::header replace "Host" [string range [HTTP::host] 4 end]
      }
    }

     

     

    Basically what this does is look to see if the Host header starts with "www." and if it does replace the header with everything after the "www." (index 4 to end).

     

     

    -Joe
  • Thanks Joe,

     

     

    I forgot to mention that I also tried it with that format:

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::host] equals "www.excel.gosolo.com" } {

     

    HTTP::header replace "Host" "excel.gosolo.com"

     

    }

     

    }

     

     

    And that did not work either? The browser still shows me http://www.excel.gosolo.com as the URL.
  • The only way to change the URL in the browser is with a redirect. The header replace, will modify the Host HTTP header that is sent to the backend server. So, the backend server will "think" that the browser requested your replaced domain while it really didn't.

     

     

    So, if you need the URL in the browser to change to reflect the correct domain, you'll have to do a redirect.

     

     

    -Joe
  • Actually, your second suggestion, the more generic recommendation, worked like a charm! The URL shows up in the browser as http://excel.gosolo.com when the irule was applied.

     

     

    Thanks!