How to pass captured group to BalanceMember using LocationMatch

47 Views Asked by At

I'm using Apache 2.4.37. I'm trying to capture part of a URL using LocationMatch and then append just the captured value to a BalanceMember entry.

Given a URL pattern like this...

https://someserver.com/some/path/:id/:moreStuff

id is a multi-character string. If it ends in a 0, I want everything in the URL after "some/path" sent to one place; else I want it sent to another place.

For example, a request to https://someserver.com/some/path/10/foo would go to https://someotherserver.com/new/path/10/foo; whereas a request to https://someserver.com/some/path/11/foo would go to https://someotherserver.com/other/path/11/foo

My first failed attempt...

# Catch-all to handle requests where id does not end in 0
<Location /some/path
  ProxyPass "balancer://other_path_balancer"
  ProxyPassReverse "balancer://other_path_balancer"
</Location>
<Proxy "balancer://other_path_balancer">
  BalancerMember https://someotherserver.com/other/path
</Proxy>

# Handle all requests where id ends in 0
<LocationMatch "^/some/path/(.*0/.*$)"
  ProxyPass "balancer://new_path_balancer"
  ProxyPassReverse "balancer://new_path_balancer"
</LocationMatch>
<Proxy "balancer://new_path_balancer">
  BalancerMember https://someotherserver.com/new/path
</Proxy>

But my example using id 10 ends up going to https://someotherserver.com/new/path/some/path/10/foo. I.e. It's appending the entire match from LocationMatch, not just the capture.

So I tried changing the LocationMatch to use a named group with a back reference...

<LocationMatch "^/some/path/(?<partIwant>.*0/.*$)"
  ProxyPass "balancer://new_path_balancer"
  ProxyPassReverse "balancer://new_path_balancer"
</LocationMatch>
<Proxy "balancer://new_path_balancer">
  BalancerMember https://someotherserver.com/new/path/%{ENV:MATCH_PARTIWANT}
</Proxy>

but this attempt yielded a request to https://someotherserver.com/new/path/%{ENV:MATCH_PARTIWANT}/some/path/10. I.e. The %{ENV:MATCH_PARTIWANT} didn't get evaluated.

I based my attempts on what I've read at How can LocationMatch and ProxyPassMatch be Combined? and https://httpd.apache.org/docs/2.4/mod/core.html#locationmatch

0

There are 0 best solutions below