Forum Discussion
Feb 05, 2010
One Vs for Redirects
This should be an easy one..
I want to use one VS for a bunch of redirects, a catch all of sorts.. If for some reason this is a bad idea just let me know.
The redirect only work for the first entry, the rest fail..
when HTTP_REQUEST {
if { [HTTP::host] eq "www.firstdomain.com" } {
HTTP::redirect "http://my.firstdomain.com"
}
elseif { [HTTP::host] eq "www.seconddomain.com" } {
HTTP::redirect "http://my.seconddomain.com"
}
}
Much appreciated
43 Replies
- L4L7_53191
Nimbostratus
This will work fine. For manageability, I'd use a data group with your host names defined. Then you can simply matchclass against it (barely tested code below!):when HTTP_REQUEST { if { [matchclass [HTTP::host] equals dc-post] } { log local0. "Got a match!" HTTP::redirect http://[string map -nocase {"www" "my"} [HTTP::host]][HTTP::uri] } else { HTTP::respond 200 content " Sorry, no match " } }
Where the data group has 'www.foo.com', 'www.bar.com' or whatever domains you want.
HTH,
Matt - How do I define the data group, also this seems to be set up just to redirect to the same domain... Can you spell out how one accomplish 3 (or more reidrects) in one iRule?
For example, redirect www.my.domain.com to www.domain.com , redirect .anotherdomain.com to www.foofoo.com, redirect .joeshouse.com to www.myhouse.com
when I tried to do it without a data group the second elseif seems to be ignored...
Thanks much apprecaite! - L4L7_53191
Nimbostratus
For this I'd use 'findclass'. First, define a data group with your redirects via the gui (iRules->Data Group List->create, give it a name and then use type 'string'). Define a space-separated list of the domains like:
www.my.foo.com www.foo.com
Where the following rule should redirect you to the www.foo.com domain if they come in with www.my.foo.com:when HTTP_REQUEST { set Host [findclass [HTTP::host] your_datagroup_name " "] if { $Host ne "" } { HTTP::redirect $Host[HTTP::uri] } }
I've not tested the iRule above but it should get you very close. I'd add an else statement to handle hosts that don't match.
HTH,
-Matt - Great thanks, that will work for redirecting to the same domain.. But what if I want to do multiple redirects and send them to multiple different domains??
redirect www.me.com to www.you.com.. etc etc..
Doing it the dirty way... My original issue is It seems to ignore the elseif statement after the first if...
What I need to accomplish is have 1 VS to handle multiple Redirects to different domains... not necessarily always the same domain..
thanks again! - Great thanks, that will work for redirecting to the same domain.. But what if I want to do multiple redirects and send them to multiple different domains??
redirect www.me.com to www.you.com.. etc etc..
Doing it the dirty way... My original issue is It seems to ignore the elseif statement after the first if...
What I need to accomplish is have 1 VS to handle multiple Redirects to different domains... not necessarily always the same domain..
thanks again! - L4L7_53191
Nimbostratus
The second domain in the data group is the destination; it doesn't have to be the same domain at all (using the most recent version of the rule posted). The first domain in the data group is the match, the second is the destination for the redirect. For example:
www.original.domain.com www.yahoo.com
www.orig2.domain.com somewhere-else.com
Stack up as many as you need in the data group!
-Matt - Gotcha, good idea on the find clasee statment. But it doesn't seem to work for me, did it work for you?
- L4L7_53191
Nimbostratus
The following rule works for me - note: I'm using version 10.1 for this testing. What version are you using? I ask because the way we reference data groups has changed and that may be affecting you.
Step 1: I created a data group called 'dc-post' and added two entries - note that I left the 'value' field blank.
"www.foo.com www.google.com"
"www.bar.com www.yahoo.com"when HTTP_REQUEST { set Host [findclass [HTTP::host] dc-post " "] log local0. "Host var is $Host" if { $Host ne "" } { HTTP::redirect http://$Host[HTTP::uri] } }
Note that there's one change to the rule above: I added the 'http://' to the redirect. It'll work either way, but my original rule was confusing (for it to work you'd need to specify 'http://www.foo.com' in your data group for the first field as opposed to just the hostname). Sorry for the confusion on that.
At any rate, this appears to be working fine. Please let me know if you still have problems.
-Matt - Ahh I'm on 9.4.8, and yes that makes more sense now with the 'http://'
I couldn't find how you define the data group in 9.4.8 vs 10.1
If you can help me out there I'd appreciate it.. and also point me to the reference material on data groups that would be helpful as well..
Thanks, much appreciated! - The_Bhattman
Nimbostratus
Here is a article explaining a bit about classes on v10
http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=2309
Here is example of using datagroups in v9
http://devcentral.f5.com/wiki/default.aspx/iRules/Pool_Member_Status_Page_on_a_Virtual_Server.html
Here is example of using datagroups in v10 in comparison
http://devcentral.f5.com/wiki/default.aspx/iRules/Pool__Member__Status__Page_on_a__Virtual__Server_v10.html
I hope this helps
Bhattman
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
