Forum Discussion
hkr_36676
Nimbostratus
Mar 04, 2008Using "switch" instead of "if"
Hi
I have made this script to remove "ndk_webste" in links
on Google.
It uses a Data_Group "map_website", to make a 301 redirect.
when HTTP_REQUEST {
/ndk_website/npsportal/cmsdoc.nsf/WebDoc/ndkw73fh32
$website == /npsportal $index == /cmsdoc.nsf/WebDoc/ndkw73fh32
if { [HTTP::uri] contains "ndk_website"} {
regexp {/ndk_website/([^/]+)} [HTTP::uri] website
regexp {(?:/cms)([^?]+)} [HTTP::uri] index
set redirect_map [findclass $website $::map_website " "]
HTTP::respond 301 Location "http://$redirect_map$index"
}
}
I have read that it is easier for the box to use switch.
How would my script look like if I used "switch"?
/Hallur
3 Replies
- The_Bhattman
Nimbostratus
I believe your code would like something like the following:when HTTP_REQUEST { /ndk_website/npsportal/cmsdoc.nsf/WebDoc/ndkw73fh32 $website == /npsportal $index == /cmsdoc.nsf/WebDoc/ndkw73fh32 switch -glob [HTTP::uri] { "*ndk_website*" { regexp {/ndk_website/[^/]+]} [HTTP:uri] website regexp {(?:/cms)([^?]+)} [HTTP::uri] index set redirect_map [findclass $website $::map_website " "] HTTP::respond 301 Location "http://$redirect_map$index" } } }
Also, I would try to replace the regexp because it evaluates much slower. Try looking at scan or findstr commands. - You beat me to it. I would absolutely agree with removing the regular expressions. The getfield and findstr commands are much more optimized for extracting strings than are regular expressions so and we recommend not using regexps unless there is no other alternative.
Using a switch -vs- findclass or matchclass is really a decision for you to make. For 100 or fewer items, a switch will perform faster, but it may be easier to maintain the data in a separate list. Only you can answer that. If you do end up going with a data group, then make sure you take into account the case where a match isn't made. In your iRule, if there is no match, you will redirect to "http://$index" which most likely won't work.
Here's My take at the iRule using a switch.when HTTP_REQUEST { if { [HTTP::uri] starts_with "/ndk_website" } { set website [getfield [HTTP::uri] "/" 3] set index [findstr [HTTP::uri] "/cms"] switch -glob $website { "nsportal" { this will match only "nsportal" set redirect_map "server1" } "*otherportal" { this will match anything ending in 'otherportal' set redirect_map "server2" } "myportal*" { this will match anything starting with 'myportal' set redirect_map "server3" } default { make sure you have a default case set redirect_map "server4" } } log local0. "URI: [HTTP::uri]" log local0. "Website: $website" log local0. "Index: $index" log local0. "Redirect: $redirect_map" HTTP::redirect "http://$redirect_map$index" } }
This code could be optimized a bit more by removing the local variables and I would definitely remove the log statements after you test this.
Hope this get's you going in the right direction.
-Joe - Don_MacVittie_1Historic F5 AccountGuys, this should really be in the iRules forums...
Moving them unless i missunderstand something?
Don.
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
