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
- Thanks guys! I think I'm close, can you show me the example with using the v9 datagroups?
Thanks! - The_Bhattman
Nimbostratus
Here is one on v9
http://devcentral.f5.com/wiki/default.aspx/iRules/AccessControlBasedOnNetworkOrHost.html
I hope this helps
Bhattman - Hmm think I'm hitting a wall here. Do you think you can work that data group into what I'm trying to do?
much appreciated.. - hoolio
Cirrostratus
The solution Matt suggested was to create a datagroup containing the HTTP host names that the client would be making requests to. He then used string map to rewrite a static string from the requested hostname to a new value and send that new value in a redirect to the client.
Is that the basic function you're looking for? If so, have you been able to create the datagroup and iRule? Is the match found in the datagroup?
Can you try this version with logging added?when HTTP_REQUEST { Log the request details and class contents. Need to use $:: to reference the class name here. log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] to [HTTP::host][HTTP::uri] with class: $::my_hosts_class" Check if the requested host is in the my_hosts_class datagroup if { [matchclass [HTTP::host] equals my_hosts_class] } { log local0. "[IP::client_addr]:[TCP::client_port]: Matched!" HTTP::redirect http://[string map -nocase {"www" "my"} [HTTP::host]][HTTP::uri] } else { HTTP::respond 200 content " Sorry, no match " log local0. "[IP::client_addr]:[TCP::client_port]: No match" } }
Make sure to update the iRule to use the name of the datagroup you've created. And I'd suggest not using a hyphen in the datagroup name as you'd then need to use curly braces to reference it.
Aaron - Stll not working, this is the log output on it;
11:05:08 tmm tmm[1144]: Rule VSred : 24.103.209.82:1172: GET to www.my.domain.com/ with class: {www.my.domain.com my.domain.com}
Feb 25 11:05:08 tmm tmm[1144]: 01220001:3: TCL error: Rule VSred - Invalid matchclass operands - no class or list type found, lhs: string, rhs: my_hosts_class (line 1) invoked from within "matchclass [HTTP::host] equals my_hosts_class"
I created a String data group with one line, www.my.domain.com my.domain.com
Thanks for all the help guys! - hoolio
Cirrostratus
Maybe the reference to the class with $:: in the log line is breaking the matchclass command? Can you remove this portion of the log line:
with class: $::my_hosts_class
and retest?
Thanks,
Aaron - Thanks for the quick reply!
This is how the rule looks now.when HTTP_REQUEST { Log the request details and class contents. Need to use $:: to reference the class name here. log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] to [HTTP::host][HTTP::uri]" Check if the requested host is in the my_hosts_class datagroup if { [matchclass [HTTP::host] equals my_hosts_class] } { log local0. "[IP::client_addr]:[TCP::client_port]: Matched!" HTTP::redirect http://[string map -nocase {"www" "my"} [HTTP::host]][HTTP::uri] } else { HTTP::respond 200 content " Sorry, no match " log local0. "[IP::client_addr]:[TCP::client_port]: No match" } }
log:
11:42:10 tmm tmm[1144]: 01220001:3: TCL error: Rule VSred - Invalid matchclass operands - no class or list type found, lhs: string, rhs: my_hosts_class (line 4) invoked from within "matchclass [HTTP::host] equals my_hosts_class"
Feb 25 11:42:31 tmm tmm[1144]: Rule VSred : 157.130.44.166:20463: GET to www.my.domain.com/
Feb 25 11:42:31 tmm tmm[1144]: 01220001:3: TCL error: Rule VSred - Invalid matchclass operands - no class or list type found, lhs: string, rhs: my_hosts_class (line 4) invoked from within "matchclass [HTTP::host] equals my_hosts_class"
Feb 25 11:42:36 tmm tmm[1144]: Rule VSred : 157.130.44.166:20585: GET to www.my.domain.com/
Feb 25 11:42:36 tmm tmm[1144]: 01220001:3: TCL error: Rule VSred - Invalid matchclass operands - no class or list type found, lhs: string, rhs: my_hosts_class (line 4) invoked from within "matchclass [HTTP::host] equals my_hosts_class" - Sorry that time I tested it looked like I had the hostnames on seperate lines in the data group, they're back to being on one line, log looks the same...
Feb 25 12:15:08 tmm tmm[1144]: Rule VSred : 209.124.48.194:28262: GET to www.my.domain.com/
Feb 25 12:15:08 tmm tmm[1144]: 01220001:3: TCL error: Rule VSred - Invalid matchclass operands - no class or list type found, lhs: string, rhs: my_hosts_class (line 4) invoked from within "matchclass [HTTP::host] equals my_hosts_class"
Feb 25 12:15:22 tmm tmm[1144]: Rule VSred : 208.125.68.192:3241: GET to www.my.domain.com/
Feb 25 12:15:22 tmm tmm[1144]: 01220001:3: TCL error: Rule VSred - Invalid matchclass operands - no class or list type found, lhs: string, rhs: my_hosts_class (line 4) invoked from within "matchclass [HTTP::host] equals my_hosts_class" - hoolio
Cirrostratus
You'll want each hostname on a separate line in the datagroup.
Are you still running 9.4.8? Is the literal name of the datagroup you've created my_hosts_class?
Can you post the anonymized output from:
b class my_hosts_class list
Thanks,
Aaron - hoolio
Cirrostratus
Or maybe it's this known issue in 9.4.x?
iRule TCL class coerced to wrong type (CR87914)
If an iRule uses a TCL class as a command, the class is coerced into a TCL command name which renders is useless for any typical class use.
Using the class in a matchclass command, causes an error as the command expects a class variable. Errors such as Invalid command name or Invalid matchclass operands are then written to the /var/log/ltm file.
Example code:
class valid_methods {
"GET"
"HEAD"
"OPTIONS"
"POST"
}
rule error_rule {
when HTTP_REQUEST {
log local0. "Hello [$::valid_methods]"
}
}
Did you have square braces around the datagroup name at some point? If so, running 'b load' should fix this.
Aaron
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
