Forum Discussion
Brent_Sachnoff_
Nimbostratus
Oct 23, 2007Using switch with uri
I'm having some issues using the switch command and uri's. I believe the switch command is only for EQUALS to something for it to work properly which means this wouldn't work at all. Here is a sample:
when HTTP_REQUEST {
switch [HTTP::uri] {
"/search/" { pool search }
"/int_search/" { pool search }
"/pictures/" { node 10.4.43.1 80 }
"/" { HTTP::redirect "https://[HTTP::host]/IMT/LoginAction.go" }
default {
pool www
}
}
}
is there a way to "match" vs equal on the uri in a switch statement?
starts_with "/search" or starts_with "/int_search/"
Thanks!
Brent
11 Replies
Sort By
- hoolio
Cirrostratus
You can use the -glob flag on switch to enable wildcards. By default, switch uses -exact to require exact matches. Here is an example:when HTTP_REQUEST { switch -glob [HTTP::uri] { "/search/*" { pool search } "*/int_search/" { pool search } "/pictures/" { node 10.4.43.1 80 } "/" { HTTP::redirect "https://[HTTP::host]/IMT/LoginAction.go" } default { pool www } } }
- John_48565
Nimbostratus
Can this take the place of an if else irule? I want to use a wildcard so that anything after /virtualdir/default/* goes to Pool_1 and then anything after /virtualdir/second/* goes to Pool_2 - Brent_Sachnoff_
Nimbostratus
jyimcse - Yes I believe that would work for you. - If you just put "/search/*" in the case for the switch, it will only hit the first part of the string as you don't have any wildcards before the first slash. But, if you used "*/search/*" it would hit both (at least I think a beginning wildcard will match on nothing...)
- Brent_Sachnoff_
Nimbostratus
I need to be able to make sure my match is coming from: - hoolio
Cirrostratus
You can test this by using a test string and a switch statement in the RULE_INIT event:when RULE_INIT { set ::test_uri /search/blah log local0. "test string: $::test_uri" switch -glob $::test_uri { /search/blah* { log local0. "matched /search/blah*" } /search/blah/foo* { log local0. "matched /search/blah*" } /search/blah* { log local0. "matched /search/blah*" } default { log local0. "didn't match any"} } }
- Brent_Sachnoff_
Nimbostratus
Aaron that gave me my answer! If we take this a step further, by inserting a cookie based on which case was matched I could use this for persistance. The F5 is throwing an error with my choice: - Brent_Sachnoff_
Nimbostratus
Any reason why my rules aren't spaced properly when I post here? I swear I put them in properly. - hoolio
Cirrostratus
You can use the \[code\] \[/code\] tags to preserve the spacing. In case that doesn't format as expected, it's [ code ] and [ /code ] without the spaces. Or you can click on the link to the right with the C on a page to have the tags inserted for you.persist cookie [insert [] []
HTTP::cookie path cc /cc
- hoolio
Cirrostratus
Actually, I'm not sure that setting the path in the HTTP_REQUEST event will work as the cookie hasn't been inserted yet. If you're able to test, and it doesn't work in HTTP_REQUEST, can you try setting the path in HTTP_RESPONSE?when HTTP_REQUEST { switch -glob [HTTP::uri] { /cc/* { set path cc do other things } rest of switch checks default { set path none do other things } } } when HTTP_RESPONSE { if {HTTP::cookie exists $path}{ HTTP::cookie path $path "/$path" } }
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