Forum Discussion
dundovic_25174
Oct 25, 2011Nimbostratus
'if' gets ignored
I wrote following iRule in attempt to redirect two specific requests to desired pages and would like to have any other request with path beginning with '/1' to get redirected to homepage:
--...
Oct 25, 2011
If is not a valid conditional test within a switch statement. If you want to add one, you'll have to do it in the "default" case.
Something like this should work:
when HTTP_REQUEST {
set uri [string tolower [HTTP::uri]]
set host [string tolower [HTTP::host]]
set link "$host$uri"
switch -exact $link {
"www.domain.com/1/2/something" {
HTTP::redirect "http://www.domain.com/something.html"
}
"www.domain.com/1/2/something2" {
HTTP::redirect "http://www.domain.com/something2.html"
}
default {
if { [HTTP::host] equals "www.domain.com" and [HTTP::path] starts_with "/1" } {
HTTP::redirect "http://www.domain.com"
}
}
}
Another way to do this would be without the if by using a wildcard comparison (-glob) in the switch like this:
when HTTP_REQUEST {
set uri [string tolower [HTTP::uri]]
set host [string tolower [HTTP::host]]
set link "$host$uri"
switch -glob $link {
"www.domain.com/1/2/something" {
HTTP::redirect "http://www.domain.com/something.html"
}
"www.domain.com/1/2/something2" {
HTTP::redirect "http://www.domain.com/something2.html"
}
"www.domain.com/1*" {
HTTP::redirect "http://www.domain.com"
}
}
Hope this helps...
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