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
- 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 " } }
- 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?
- 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:when HTTP_REQUEST { set Host [findclass [HTTP::host] your_datagroup_name " "] if { $Host ne "" } { HTTP::redirect $Host[HTTP::uri] } }
- 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??
- 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??
- 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: - 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.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] } }
- Ahh I'm on 9.4.8, and yes that makes more sense now with the 'http://'
- The_Bhattman
Nimbostratus
Here is a article explaining a bit about classes on v10
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