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.
9 Replies
- Chris_Miller
Altostratus
You basically want to get rid of the 10 or 3 after the www? - Hai_Le_36697
Nimbostratus
hello,
Yes, that's what I want to get rid of. - 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.
when HTTP_REQUEST {
if {[HTTP::host] starts_with "www10." or "www3."} {
{
set key "[concat [domain [HTTP::host] 2]]"
HTTP::respond 301 Location "http://www.$key[HTTP::uri]"
}
}
}
Thank you for your time. - 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?
As for the latest example you posted:
You can only check one string against another string using starts_with. So you can change this:if {[HTTP::host] starts_with "www10." or "www3."} {
to:if {[HTTP::host] starts_with "www10." or [HTTP::host] starts_with "www3."} {
Or you could use a switch statement instead:switch -glob [string tolower [HTTP::host]] { "www3.*" - "www10.*" { HTTP::respond 301 Location "http://www.[domain [HTTP::host] 2][HTTP::uri]" } }
And you shouldn't need to use concat with the output from the domain command. domain [HTTP::host] 2 should return the last two fields of the domain delineated by periods. So you can change this:set key "[concat [domain [HTTP::host] 2]]" HTTP::respond 301 Location "http://www.$key[HTTP::uri]"
to:HTTP::respond 301 Location "http://www.[domain [HTTP::host] 2][HTTP::uri]"
Aaron - Hai_Le_36697
Nimbostratus
Hi Aaron,
The first scenario is correct. I want to redirect all requests from www10.domain.com or www3.domain.com to www.domain.com. It doesn't matter what the domain is. We have about 15 domains that have I am looking to redirect back to www so I would like to make it as generic as possible.
I'll look at implementing your suggested changes.
THANK YOU! - 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? } } }
Aaron - Hai_Le_36697
Nimbostratus
Hi aaron,
I want to give you the full story. The reason I am writing this iRule is to redirect bots like Googlebot to the correct URL. www3 and www10 are our backdoor virtual hosts and I don't want bots to index them (which they are). With that said, I did take both of your suggestions with my latest attempt. Here it is:
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::host]] {
"www2.*" -
"www3.*" -
"www10.*" {
switch -glob [string tolower [HTTP::header User-Agent]] {
"googlebot*" -
"mediapartners*" -
"msnbot*" -
"slurp*" -
"ia_archiver*" -
"zyborg*" -
"askjeeves*" {
HTTP::respond 301 Location "http://www.[domain [HTTP::host] 2][HTTP::uri]"
}
}
}
}
}
So basically, if the the request is for www2, www3, or www10 and the agent is an identified bot, then do a 301 redirect to www, otherwise don't do anything.
I manage the dns for my domains and I know that I only use ww2, www3, and www10 so the first switch would work perfectly.
We just want to increase our www page ranks in google and other search engines by redirecting all the www2, www3, and www10 hosts.
I hope that makes more sense. Sorry for not filling you in on the full story... I just didn't want to add confusion to my current problem with extracting the domain from HTTP::host
Any comments would be greatly appreciated.
Hai - hoolio
Cirrostratus
Hi Hai,
Based on your explanation of the scenario, the updated iRule looks perfect. nice work!
Aaron - Hai_Le_36697
Nimbostratus
Excellent! Couldn't have done it without your valued feedback. Thanks again!
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
