Forum Discussion
Adding Catchall Behavior to iRule
With the following iRule, when I hit domain.com/ShortName1_Anything here, it will keep the URI portion intact and pass it to the default pool for the VIP.
if { [HTTP::uri] eq "/ShortName1*" } {
}
elseif { [HTTP::uri] eq "/BLAH/ShortName1*" } {
HTTP::uri "/ShortName1"
pool CUSTOM_POOL2
}
My company wants a catch all if the user does not type in a valid /ShortName to redirect to a default one. When I add the following section of code, it states there is a redirect loop in my browser:
elseif {([HTTP::host] contains "sub.domain.com") && !([HTTP::uri] eq "/ShortName1*")} {
HTTP::redirect "/ShortName1"
pool CUSTOM_POOL1
}
I have tried several different approaches from browsing in the forums, and cannot seem to get a working catch-all.
The goal is to have all traffic that is does not resolve to /ShortName1 or /ShortName2, to default to /ShortName1.
Am I missing something or perhaps there is an easier way to do this? Thank you.
- Michael_JenkinsCirrostratus
I think the issue is with your
in your if statement. Try this instead...*
switch -glob -- [string tolower [HTTP::uri]] { "/shortname1*" { Do nothing } "/blah/shortname*" { HTTP::uri "/ShortName1" pool CUSTOM_POOL2 } default { HTTP::redirect "/ShortName1" If you perform the redirect there's no reason to set the pool, because the client will redirect. } }
Of course, you can make changes like if you need to maintain the original URI after the /shortname or if you want to add more options.
Hope this helps
- Mark_S__182830Nimbostratus
That worked great - so when you said you think my * was the issue, did you mean here:
&& !([HTTP::uri] eq "/ShortName1*")
Thanks a million!
- Michael_JenkinsCirrostratusYea. the "eq" doesn't allow wildcards, so you would have either had to use "string match" or the switch statement (which to me is easier to read than the if statements).
- Mark_S__182830Nimbostratus
I owe you a beer - thanks a lot Michael.
- Michael_JenkinsCirrostratusGlad I could help. It's nice to have a place like DevCentral where we can all get and give answers :)
- Chris_WentlandNimbostratus
You could have also switched the operator from equals to starts_with, eliminate the * from the matching string, and end the if/elseif with an else statement to set the default action. The switch command is a more efficient way to do the same thing, and uses the * in the URI match to equivocate it to the starts_with operator.
- Mark_S__182830NimbostratusThanks Chris! I'm learning a lot as I run into issues :)
- Chris_Wentland_Nimbostratus
You could have also switched the operator from equals to starts_with, eliminate the * from the matching string, and end the if/elseif with an else statement to set the default action. The switch command is a more efficient way to do the same thing, and uses the * in the URI match to equivocate it to the starts_with operator.
- Mark_S__182830NimbostratusThanks Chris! I'm learning a lot as I run into issues :)
- Chris_WentlandNimbostratus
if { [HTTP::uri] starts_with "/ShortName1" } { } elseif { [HTTP::uri] starts_with "/BLAH/ShortName1" } { HTTP::uri "/ShortName1" pool CUSTOM_POOL2 } else {([HTTP::host] contains "sub.domain.com") { HTTP::redirect "/ShortName1" }
Recent Discussions
Related Content
* 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