mod_rewrite and mod_proxy conflicts

I do a lot of apache rewrite rules at work and have typically relied on using the [P] proxy flag to proxy content. However, this has performance implications since
it does not pool connections. In fact Apache documentation specifically mentions this
concern.

The solution was to change to use ProxyPassMatch

1
2
3
4
5
6
7
# Go from this:
RewriteRule ^/test/(.*)$ http://proxy/$1 [P]
ProxyPassReverse /test
# To this:
ProxyPassMatch ^/test/(.*)$ http://http://proxy/$1
ProxyPassReverse /test http://http://proxy/test

However, we ran into an issue with this. With RewriteRule doing the proxying you control the flow of rewrites. With ProxyPassMatch they are executed AFTER.
The solution to this was to escape any rewrite rules before doing the proxy.

1
2
3
RewriteRule ^/test/(.*)$ - [END]
ProxyPassMatch ^/test/(.*)$ http://http://proxy/$1
ProxyPassReverse /test http://http://proxy/test

Now any reverse proxies work as expected without rewrite rule processing before them.