Why do relative mod_rewrite substitutions redirect differently behind a production reverse proxy than on localhost?
0 reputation · 06 Apr 2020, 03:52 UTC
I have a set of mod_rewrite rules in a VirtualHost on Apache httpd 2.4 that use relative substitutions, for example a pattern like ^(.*)$ /app/$1. On my local machine, where the site is served directly from its DocumentRoot, the rewritten URLs resolve exactly as expected.
In production the same configuration sits behind a reverse proxy, and the rewritten targets appear to be evaluated against a different base. Depending on the request, I see redirects pointing at the wrong path segment, and in some cases what looks like a rewrite loop. The rules themselves are identical in both environments; only the DocumentRoot layout and the proxy layer differ.
My understanding is that per-server and per-directory rewrite contexts resolve relative substitutions differently, and that RewriteBase interacts with this, but I am not sure how the proxy changes which context applies or whether I should be forcing absolute substitutions with the [R] flag or a leading scheme and host.
Which context (server, VirtualHost, directory, or .htaccess) determines how a relative substitution is resolved, and does a fronting reverse proxy alter that resolution? Should relative substitutions be avoided entirely in proxied deployments, or is setting an explicit RewriteBase sufficient to make behavior identical across environments?