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_we...
Mar 04, 2008
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
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