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:
---
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"}
if { [HTTP::host] equals "www.domain.com" and [HTTP::path] starts_with "/1" } then {
HTTP::redirect "http://www.domain.com"
}
}
}
---
The explicit redirects work fine, but the 'if' statement that checks for '/1' in path start gets totally ignored. Could somebody clarify why?
Regards,
D.
- 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.
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" } } }
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" } }
- dundovic_25174NimbostratusThanks a lot, Joe!
- No problem, hope it helps!
- dundovic_25174NimbostratusThe 'switch -glob' method is absolutely perfect and works like a charm. Also seems the way it's written takes less CPU processing. Thanks, again!
- Glad to hear it! If you haven't seen this yet, check out the "iRules 101" series of articles we've written. Lots of good stuff in there on the basics of the various commands: http://devcentral.f5.com/wiki/iRules.iRules101.ashx
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