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
- The_Bhattman
Nimbostratus
Hi iRuleYou,
Sounds like your piece of code will benefit from findclass (Click here)
I hope this helps
Bhattman - Ahh Now I see what happened.. we started off with findclass and started using matchclass along the way and I should of read the syntax for matchclass..
So it looks like I have to use the findclass which has the key value..
Thanks Bhatman, I'll give it a try and let ya know how I make out. - hoolio
Cirrostratus
Can you try something like this with a datagroup containing the original host header value, a space and then the host to redirect to on a single line?when HTTP_REQUEST { Log the request details 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 The my_hosts_class should be a datagroup containing the host to match on and the host to redirect to separated by a space: host_orig_1 host_new_1 host_orig_2 host_new_2 set host [findclass [string tolower [HTTP::host]] $::my_hosts_class " "] Check if there was a match if {$host ne ""}{ log local0. "[IP::client_addr]:[TCP::client_port]: Matched $host" HTTP::redirect "http://$host[HTTP::uri]" } else { HTTP::respond 200 content " Sorry, no match " log local0. "[IP::client_addr]:[TCP::client_port]: No match" } }
Aaron - Sweet, working like a charm!
Just a few quick questions;
- in the set statment, $host will only get created if there is a match, correct?
-You're also setting the hosts url to all lower case with "string tolower [HTTP::host]", correct?
-what is the " " for after you call the my_hosts_class datagroup?
-I see you used a "ne" for the compare, it's supposed to be faster than an eq?
-When you say "if {$host ne ""}{ " you're saying if $host has any value continue and redirct to $host which now contains the value after the space in the line it matched, correct?
-why do you need the [HTTP::uri] after the $host in the redirect?
Thanks man, sorry for all the questions, I really appreciate the help, just want to make sure I understand it.. - hoolio
Cirrostratus
No problem. It's good that you're making sure you understand how the rule works.
findclass evaluates each line of the datagroup (class) one at a time. It checks the string provided (the host header value set to lowercase) against the first field of the line (with fields delineated by a space: " "). If there is a match, findclass returns the second field in the matching datagroup line. If there is no match, findclass returns a null length string. So $host will be set to either the second field of the match line or nothing.
ne means not equal. So we're checking if $host isn't null with ($host ne "").
I included [HTTP::uri] after the $host in the redirect to include the originally requested URI in the new location being sent to the client. If you just want to redirect the client to the new hostname from the datagroup without a URI, you can take off [HTTP::uri] from the HTTP::redirect command.
For details on the findclass command and TCL operators, you can check the findclass wiki page and the expr man page:
http://devcentral.f5.com/wiki/default.aspx/iRules/findclass
http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htmM13
Aaron - hoolio
Cirrostratus
We'd discussed the efficiency of using string operators on strings in this post, and seen some mixed results:
Efficiency of String Comparison
http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&tpage=1&view=topic&postid=1144749
I just tried testing again with the time command in tclsh, and it does seem more efficient to use eq/ne to compare strings that aren't the same. I'll try testing again in an iRule at some point.
As for TCL references, I find the man pages on http://www.tcl.tk/man/tcl8.4/TclCmd/X.htm (where X is the command name) and the wiki pages on http://wiki.tcl.tk to be very useful. Because much of the advanced functionality of TCL is disabled for iRules, the main complexity is generally with the F5 specific functionality. So I've found the forum posts and iRule wiki pages to be more useful than books for iRules.
If you find any gems elsewhere, it would be great if you could write about them here.
Thanks,
Aaron - Great thanks man!
Is it spelled out anywhere what is disabled and what is added for iRules in TCL?
Yep I'll be sure to post anything else I find.. - hoolio
Cirrostratus
Ask and ye shall receive:
http://devcentral.f5.com/wiki/default.aspx/iRules/DisabledTclCommands.html
Aaron - Thanks man
- Justin_Adrian_3
Nimbostratus
Not sure if you are done with this, but I built this little irule to do a similar function. I wanted to avoid having to modify one massive data set. Thought that smaller data sets that could be added as needed would work better.Master Vanity IRule Created 8/5/09 by Justin Adrian This iRule is built to redirect header requests It will take the host name and look for a data set with the following name VURL_host.domain.com The data set should have a string value of the redirect when HTTP_REQUEST { log local0.info [findclass "http" [set ::VURL_[HTTP::host]]] redirect to URL in data set HTTP::redirect [findclass [string tolower [HTTP::uri]] [set ::VURL_[string tolower [HTTP::host]]] " "] }
Then for each new site/redirect that I create, I create a data set like below.
The name includes VURL then the actual host string.
The string is built with what you are matching on, and then the corresponding redirect.
I have successfully had this work with 150 matches with unique redirects in a single data set.class VURL_mx.test.com { "/ https://mx.test.com/testclaimapp?whr=badstuff" } class VURL_mytest.test.com { "/ https://mytest.test.com/administration/Default.aspx" } class VURL_pretest.test.com { "/ https://pre.test.com/opensso/WSFederationServlet/metaAlias/tdcauthweb?goto=https://sso-stage.test.com/opensso" } class VURL_tgpp.test.com { "/ https://kxo.test.com/Organizations/Pages/SIandT-Podcasts.aspx" }
-J
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
