Apache reverse proxy double trailing slash

apache-2.4Apache2mod-rewritereverse-proxy

I have a reverse proxy setup in my virtual host. However, when I visit the site: https://example.com instead of https://example.com/ in the browser address bar, I get https://example.com//. Why do I have two trailing slashes? Is there something wrong with my apache conf? (supplied below) or is the trailing slash something that might have been added by apache conf/.htaccess by a site that is behind a reverse proxy?

My current setup:

<VirtualHost *:80>
    ...
    RewriteEngine On
    RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</VirtualHost>
<VirtualHost *:443>
    ...
    ProxyRequests Off
    SSLProxyEngine on
    ProxyPass / https://example.proxy.com/
    ProxyPassReverse / https://example.proxy.com/
</VirtualHost>

Best Answer

when I visit the site: https://example.com instead of https://example.com/ in the browser address bar, I get https://example.com//.

These two requests, with and without the trailing slash, are really the same thing. The browser will send the same request to your server in both cases. So, it's difficult to see why you would get a different response in these two cases, unless there was some local caching issue?

(See my answer to the following question on the Pro Webmasters stack for more information with regards to how the browser handles the trailing slash on the domain: https://webmasters.stackexchange.com/questions/35643/is-trailing-slash-automagically-added-on-click-of-home-page-url-in-browser)

However, the RewriteRule directive in your <VirtualHost *:80> container will always result in a double slash - so maybe you are seeing a cached response (or browser auto-completion) from this earlier (erroneous) redirect? Note that 301 redirects are cached hard by the browser.

RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

In a virtualhost context, the $1 backreference will capture the slash prefix on the URL-path (this differs from a directory or .htaccess context).

You need to either remove the slash in the substitution, or take the first slash out of the captured pattern (or use the REQUEST_URI server variable). For example:

RewriteRule ^/(.*) https://example.com/$1 [R=301,L]

(No need for the end-of-string anchor, since * is greedy by default.)

And make sure your browser cache is cleared.