Forum Discussion

Russell_Findley's avatar
Russell_Findley
Icon for Nimbostratus rankNimbostratus
Jan 23, 2006

Redirect Rule from WWW to Unique URL

I am unable to train our customers to stop using WWW in front of their url so I wanted to write a rule to redirect them, but I need some help with the syntax.

 

 

I'm trying to do the following.

 

 

Redirect www.x.foo.com to x.foo.com

 

 

This rule would only be applied to one virtual node on our bigip.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    I think what you're looking for would look something like this:

    
    if (http_uri starts_with "www") {
      redirect to "http://something.%h/%u/"
    }

    -Colin
  • Let me be more specific because the rule didn't work.

     

     

    https://www.x.foo.com need to redirect to https://x.foo.com

     

     

    Does that change the syntax?

     

     

    --Russ
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    That does change things a bit.

    Try this one:

    
    if (http_host starts_with "www.") {
      redirect to (substr(http_host, 4) + http_uri)
    }

    See if that works a little better.

    -Colin
  • Sorry, but I'm not familiar with this syntax.

     

     

    (substr(http_host, 4) + http_uri)

     

     

    Is there somewhere I can read what this does or can you explain?
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Well, you can read more about it here:

     

     

    Click here

     

     

    But, I'll be happy to explain a bit.

     

     

    Basically, the substr command returns the string in question, starting at the offset given. So, that command should return the htt_host, starting at the fourth character.

     

     

    This should turn www.x.domain.com into x.domain.com, since it skips the "www.".

     

     

    -Colin
  • It's almost like the Bigip isn't taking the new rule when I apply it. Should I do a b load to update the config?
  • If I use if

     

     

    (http_host starts_with "www.") { redirect to (substr(http_host, 4) + http_uri)}

     

     

    Which part of this should I edit with the url that I want?

     

     

    Russ
  • Martin_Machacek's avatar
    Martin_Machacek
    Historic F5 Account
    Russell,

    Let's step back first for a second ... so, what you want to do is redirect requests to https://www.x.foo.com to https://x.foo.com, right?

    If you don't mind "redirecting" also http://www.x.foo.com to http://x.foo.com and assuming that you are in control of your DNS domain, then the absolutely easiest way how to do that is to change the DNS entry for www.x.foo.com to be an alias (PTR) for x.foo.com, or at least resolve to the same address (address of x.foo.com) and it does not matter whether user put http://www.x.foo.com or http://x.foo.com to the URL (assuming that your original servers are not doing any funky matching on the Host header in requests).

    OK, if you cannot use the solution above, then you can do redirection on the BIG-IP, but in case of HTTPS, you'd have to configure the BIG-IP to terminate the SSL connections (i.e. use the SSL proxy). Otherwise BIG-IP cannot see the content of the connection because it is encrypted and cannot issue the redirect.

    Assuming that you've configured SSL proxy and make it direct traffic to the same virtual that handles HTTP connections for the same address, and assuming that only www.x.foo.com resolves to the address of the proxy (HTTPS case) which is the same as the address of a port 80 virtual (I further denote the address as , then the configuration can be as easy as this:

    
    rule redirect_to_x.foo.com {
       redirect to "x.foo.com" + http_uri
    }
    virtual :80 {
       use rule redirect_to_x.foo.com
    }
    proxy :443 {
       
       target virtual :80
    }

    Why it works without matching the hostname? Because the client has to do DNS resolution for www.x.foo.com to figure out to which address to connect. So, only clients that requesting http://www.x.foo.com/ connect to the virtual server and there is no need to match the http_host. Similarly only clients requesting https://www.x.foo.com/ connect to the proxy.

    If there are multiple DNS names that resolve to (i.e. a virtual hosting scenario), then you need to match the hostname and your rule needs to look like this:

    
    rule redirect_to_x.foo.com {
       if(http_host == "www.x.foo.com") {
          redirect to "x.foo.com" + http_uri
       } else {
          use pool 
       }
    }