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
- 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" } } }
- 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.
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" } }
- Don_MacVittie_1Historic F5 AccountGuys, this should really be in the iRules forums...
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