Forum Discussion
Hai_Le_36697
Nimbostratus
Sep 24, 2010redirect wwwX.domain.com to www.domain.com
Hey guys,
I'm pretty new to iRule and this forum has really helped me get to where I am; however, I am stuck and I hope you guys can help. What I would like to do is to extract the domain name from the request and redirect users to www.domain.com. Here is what I've got fo far:
when HTTP_REQUEST {
if {[HTTP::host] starts_with "www10." or "www3."}
{ HTTP::respond 301 Location "http://www.DOMAIN [HTTP::uri]" }
}
Please let me know if there is a way replace the domain with my domain name.
- Chris_Miller
Altostratus
You basically want to get rid of the 10 or 3 after the www? - Hai_Le_36697
Nimbostratus
hello, - Hai_Le_36697
Nimbostratus
I believe I have found the solution to my problem! Please let me know what you guys think and if you see any gotchas. - hoolio
Cirrostratus
Can you provide a bit more detail on your requirements? Do you always want to send a redirect to one hostname (like www.example.com) if the client makes a request with a hostname that isn't www.example.com? Or are there multiple domains that clients could request which resolve to the same virtual server IP address? If there are multiple, do you always want to take the last two domain parts from the request and add www. to the front to form the host in the redirect?if {[HTTP::host] starts_with "www10." or "www3."} {
if {[HTTP::host] starts_with "www10." or [HTTP::host] starts_with "www3."} {
switch -glob [string tolower [HTTP::host]] { "www3.*" - "www10.*" { HTTP::respond 301 Location "http://www.[domain [HTTP::host] 2][HTTP::uri]" } }
set key "[concat [domain [HTTP::host] 2]]" HTTP::respond 301 Location "http://www.$key[HTTP::uri]"
HTTP::respond 301 Location "http://www.[domain [HTTP::host] 2][HTTP::uri]"
- Hai_Le_36697
Nimbostratus
Hi Aaron, - hoolio
Cirrostratus
Here's something that might work for you. I'm not sure what you want to do if the client either doesn't set a host header value or uses an IP address, so I've just left a comment in the default switch case.when HTTP_REQUEST { Check requested host set to lowercase switch -glob [string tolower [HTTP::host]] { "www.*" { Host starts with www. so exit this event in this iRule return } "*[a-z]*" { Host isn't an IP and didn't start with www., so send permanent redirect HTTP::respond 301 Location "http://www.[domain [HTTP::host] 2][HTTP::uri]" } default { Host is either blank or an IP address, do something? } } }
- Hai_Le_36697
Nimbostratus
Hi aaron, - hoolio
Cirrostratus
Hi Hai, - Hai_Le_36697
Nimbostratus
Excellent! Couldn't have done it without your valued feedback. Thanks again!
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