Forum Discussion
Russell_Findley
Nimbostratus
Jan 23, 2006Redirect 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.
8 Replies
- Colin_Walker_12Historic F5 AccountI think what you're looking for would look something like this:
if (http_uri starts_with "www") { redirect to "http://something.%h/%u/" }
-Colin - Russell_Findley
Nimbostratus
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_12Historic F5 AccountThat 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 - Russell_Findley
Nimbostratus
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_12Historic F5 AccountWell, 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 - Russell_Findley
Nimbostratus
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? - Russell_Findley
Nimbostratus
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_MachacekHistoric F5 AccountRussell,
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 } }
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects