Forum Discussion

Bob_10976's avatar
Bob_10976
Icon for Nimbostratus rankNimbostratus
Jan 23, 2012

Limiting WWW redirect to just WWW

Hello all...

We have an irule in place that redirects www.domain.com to non domain.com version. This seems to work fine with one exception. If the end user goes to www.domain.com/blog then they are redirected back to the home site of domain.com. What I'd like to do is when someone goes to www.domain.com/blog (or / whatever) that the www remains in place, but if they go to the root of the site, www.domain.com that they end up at domain.com.

Below is the what we currently use, any suggestions on how I can modifiy this?

 www.domain.com {
HTTP::respond 301 Location http://domain.com
}

Thanks,

Bob

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    It sounds like you're looking for is for the redirect to include the URI, which is something we've seen many times using the redirect command, and would change very little in your case using the respond command:

    
    when HTTP_REQUEST {
      if { [HTTP::host] eq "www.domain.com"} {
        HTTP::respond 301 Location "http://domain.com[HTTP::uri]"
      }
    }
    

    In this fashion whatever URI the user requested along with the hostname would remain intact after the redirect.

    Colin
  • Colin,Thanks! That sounds like what I want to do excatly. However I do have one follow up question. We host a lot of sites and I'm using an irule format that I'm not sure how I could incorporate your syntax into the current rulle. Below is an example of the rule we have in place..

    
    when HTTP_REQUEST {
    switch "[string tolower [HTTP::host]]" {
    "domain1.com" - 
    www.domain1.com {
     HTTP::respond 301 Location http://domainName.com
    }
    "domain2.com" - 
    www.domain2.com {
     HTTP::respond 301 Location https://domain2.com
    }
    www.domain.com {
     HTTP::respond 301 Location "http://domain.com/" 
    }
    "domain3.com" {
     HTTP::respond 301 Location "https://domain3.com/" 
    }
    "domain4.com" {
    if {[string tolower [HTTP::uri]] eq "/aer/"}{
     HTTP::respond 301 Location "http://domain4.com/artsdirectory.aspx"
     }
    ETC.. ETC.. ETC..

    I'm not sure how I would add that into the rule if the host I want to apply this to is say the 3rd one down. Any thoughts?v
  • Thanks Colin.. After a few trails I got it figured out, not as hard as I thought it would have been..