Forum Discussion

Leo's avatar
Leo
Icon for Nimbostratus rankNimbostratus
Mar 01, 2012

Changing the URL

Hello friends,

 

I would like to add "www" to url's without it. meaning, if someone goes to "http://example.com/page.html" the F5 will redirect it to "http://www.example.com/page.html".

 

I've made the following iRule".

 

 

when HTTP_REQUEST {

 

if {[HTTP::host] ne ""} and {!{[HTTP::host] contains "www."}} {

 

HTTP::respond 301 Location "http://www.[getfield [HTTP::host]][HTTP::uri]"

 

}

 

}

 

 

I'm not so sure about the syntax, how does it look?

 

  • Hi Leonid,

    I don't think that you need to bother checking to see if the [HTTP::host] is empty. If it is empty then it will never hit the Virtual Server. It could be an IP Address or something else but should never be empty.

    This will do what you were wanting, but will not function properly if you hit the Virtual Server with an IP Address. If you want any traffic hitting the Virtual Server with an IP Address to respond properly you will need to add additional logic.

    
    when HTTP_REQUEST {
    if { !([HTTP::host] starts_with "www") } {
    HTTP::respond 301 Location "http://www.[getfield [HTTP::host] ":" 1][HTTP::uri]"
    }
    }
    

    Hope this helps.
  • Leo's avatar
    Leo
    Icon for Nimbostratus rankNimbostratus
    Thank's for the quick reply,

     

    The code works well for HTTP requests, but when I try to do the same to HTTPS traffic - it redirects me to HTTP for some reason (I've obviously changed the protocol to HTTPS in the redirect... any idea what i'm doing wrong? (ssl terminates on the F5 machine...)
  • You could use a browser plugin like Fiddler2 to check what redirects/references to http:// the browser is getting.

     

     

    Is your cert valid for example.com as well as www.example.com? If not, the client will get a mismatched cert warning for HTTPS requests.

     

     

    Aaron
  • Leo's avatar
    Leo
    Icon for Nimbostratus rankNimbostratus
    The certificate is fine.

    I using HttpFox to see the redirects - and I can see that the redirect is fine - but not on the correct protocol!

    I'm using the following on the HTTPS VIP:

    when HTTP_REQUEST {
        if { !([HTTP::host] starts_with "www") } {
            HTTP::respond 301 Location "https://www.[getfield [HTTP::host] ":" 1][HTTP::uri]"
        }
    }
  • Leo's avatar
    Leo
    Icon for Nimbostratus rankNimbostratus
    Apparently, it only happened on Firefox on my computer - other browsers and computers work well. Weird - but fine by me.

     

    Thanks for the help everyone.