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.
10 Replies
- Michael_Jenkins
Cirrostratus
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__182830
Nimbostratus
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_Jenkins
Cirrostratus
Yea. 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__182830
Nimbostratus
I owe you a beer - thanks a lot Michael.
- Michael_Jenkins
Cirrostratus
Glad I could help. It's nice to have a place like DevCentral where we can all get and give answers :)
- 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__182830
Nimbostratus
Thanks 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__182830
Nimbostratus
Thanks Chris! I'm learning a lot as I run into issues :)
- Chris_Wentland
Nimbostratus
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" }
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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