blackbird
blackbird

Reputation: 1137

Apache URL rewrite before Proxy

I have apache set up as a proxy to my application that needs to run on a specific port, this works fine with virtual host. Now I need to add another directive, I need to remove /ex/ from any incoming request and pass the rewritten url it to the proxy.

I can't seem to get both to work, either apache is able to rewrite the url but then fails to proxy and tries to serve the request itself, or it proxies correctly without removing /ex/ and my application routing fails because it's looking for /ex/.

Here's the proxy config that works (without the rewrite). How can I remove /ex/ before passing it to the proxy ? Apparently apache can't [P,R] at the same time, PT simply forwards as is.

<VirtualHost *:82>
  ServerName xxx
  ServerAlias xxx

  DocumentRoot /opt/xxx

  RewriteEngine On

  # neither of these works, simply proxies as is to my application, routing fails
  # RewriteRule ^ex/(.*) /$1 [L,R]
  # RewriteRule ^/ex$ / [L,PT]

  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ http://127.0.0.1:11110%{REQUEST_URI} [P,QSA,L]

  ProxyPass / http://127.0.0.1:11110/
  ProxyPassReverse / http://127.0.0.1:11110/
  ProxyPreserveHost on

  # this doesn't work either
  # ProxyPass /ex http://127.0.0.1:11110/

</VirtualHost>

Upvotes: 0

Views: 2484

Answers (2)

punitusingh
punitusingh

Reputation: 35

Simple, this line

ProxyPass /ex http://127.0.0.1:11110/

should come before

ProxyPass / http://127.0.0.1:11110/

as / will match everything, it is bigger bucket then /ex, so ProxyPass / should always be last in the configuration file. That means if no matching /pattern found then / will handle all the pattern at the end.

Upvotes: 0

covener
covener

Reputation: 17872

You could just use ProxyPassMatch instead of ProxyPass:

ProxyPassMatch ^/ex/(.*) http://127.0.0.1:11110/$1

Upvotes: 1

Related Questions