Forum Discussion
Best way to rewrite and choose different pool at same time
What would be the best method to achieve the following:
If incoming url starts with "/test" replace /test with just "/" and choose pool "test"
I've done some of my other pool selection with LTM policies but I'm concerned that I can't make the policy replace just the "/test" part...I don't want it to do something like replace "/test/api/call1" with "/" and then the app running on node test breaks because it's expecting the request to be "/api/call1"
11 Replies
- ekaleido
Cirrus
Assign one iRule to do the pool selection, then apply ProxyPass to handle the rewrite.
- VernonWells
Employee
when HTTP_REQUEST { if { [HTTP::path] equals "/test" } { HTTP::path "/" pool test } }This will match exactly (and only) /test and nothing else.
- JWhitesPro_1928
Cirrostratus
I will test this but will it effectively just remove the /test so that if a user goes to:
myserver.com/test/documents/clinton.pdf that it would rewrite it to just /documents/clinton.pdf because that is what the backend pool server would be expecting (it wouldn't have a path for /test)
- Josh_Jacobson_4
Altostratus
You can use [HTTP::uri] to preserve any GET vars (if you don't care about GET vars [HTTP::path] is fine too of course). Here's one way to strip the first segment of the path and send the rest on. I broke it out into two statements in case that's easier to read, and there's a one-liner version of it at the end too.
when HTTP_REQUEST { if { [HTTP::uri] starts_with "/test/" } { This will find the position of the second slash (starts searching at position 1; first slash is position 0) set slashpos [string first / [HTTP::uri] 1] Substring from the position of the second slash through the end of the URI set newuri [substr [HTTP::uri] $slashpos] Here's the one-liner to accomplish the same thing (it was just easier to comment as two statements): set newuri [substr [HTTP::uri] [string first / [HTTP::uri] 1]] log local0. "New URI: $newuri" HTTP::uri $newuri pool test } } - Josh_Jacobson_4
Altostratus
oops, looking at Vernon's answer below I should have put in a test for no second slash - sorry if my answer does more harm than good!
-Josh
- Vernon_97235Historic F5 Account
when HTTP_REQUEST { if { [HTTP::path] equals "/test" } { HTTP::path "/" pool test } }This will match exactly (and only) /test and nothing else.
- JWhitesPro_1928
Cirrostratus
I will test this but will it effectively just remove the /test so that if a user goes to:
myserver.com/test/documents/clinton.pdf that it would rewrite it to just /documents/clinton.pdf because that is what the backend pool server would be expecting (it wouldn't have a path for /test)
- Josh_Jacobson_4
Altostratus
You can use [HTTP::uri] to preserve any GET vars (if you don't care about GET vars [HTTP::path] is fine too of course). Here's one way to strip the first segment of the path and send the rest on. I broke it out into two statements in case that's easier to read, and there's a one-liner version of it at the end too.
when HTTP_REQUEST { if { [HTTP::uri] starts_with "/test/" } { This will find the position of the second slash (starts searching at position 1; first slash is position 0) set slashpos [string first / [HTTP::uri] 1] Substring from the position of the second slash through the end of the URI set newuri [substr [HTTP::uri] $slashpos] Here's the one-liner to accomplish the same thing (it was just easier to comment as two statements): set newuri [substr [HTTP::uri] [string first / [HTTP::uri] 1]] log local0. "New URI: $newuri" HTTP::uri $newuri pool test } } - Josh_Jacobson_4
Altostratus
oops, looking at Vernon's answer below I should have put in a test for no second slash - sorry if my answer does more harm than good!
-Josh
- VernonWells
Employee
Ah, I see. You want to remove /test for an Request Path that is /test or begins with /test/, correct? If so:
when HTTP_REQUEST { switch -glob [HTTP::path] { "/test" { HTTP::path "/" pool test } "/test/*" { HTTP::path "[substr [HTTP::path] 5]" pool test } } } - VernonWells
Employee
does preserve any Request Target parameters. That is, in order to retain the query parameters, the use of a reconstructedHTTP::path
is not needed.HTTP::uri
affects only the path portion of the Request Target. As such, if I submit this request:HTTP::pathGET /test/abc/def.html?this=that&here=there HTTP/1.1 ...my rule above would transform the Request Target to:
GET /abc/def.html?this=that&here=there HTTP/1.1 ...
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