大网数据广告活动
服务器运维问答

Apache reverse proxy double trailing slash

· 52CE 我爱测

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…

问题原文

原帖链接 · Gitnik · CC BY-SA 3.0

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>

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。