https 后面的 Apache 目录尾随斜线问题
Apache directory trailing slash issue behind https
我 运行 一个位于 https 门户后面的 apache 服务器并且目录重定向有问题。
我的 apache 服务器是一个 docker 容器,它接收由 https 门户容器 (steveltn/https-portal) 转发的请求。
一切正常,除了对 http 而不是 https 进行的默认 http 重定向。例如,假设我们在我的 apache 网站中有一个名为 test 的目录。呼叫 https://example.com/test returns a 301 code with redirection to http://example.com/test/。
正确的行为是重定向到 https。
我首先认为这是我的 https 门户配置错误,并询问了 steveltn/https-portal 团队。但他们回答说这是我的 apache 配置问题 (https://github.com/SteveLTN/https-portal/issues/67#issuecomment-257934618)。
答案摘要是
PORTAL does tell the Apache about its existence by the request header
X-Forwarded-Proto: https. Some web apps recognize this header
automatically, such as WordPress. I guess now it up to you to
configure your web app to recognize this header
我尝试了很多在 Internet 上找到的配置,例如这个,但是 none 解决了问题:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
如果我将 RewriteCond 更改为 %{HTTP:X-Forwarded-Proto} https,我会得到一个无限重定向循环。
有什么想法吗?
可能是您的 .htaccess 文件导致了此重定向?
如果问题确实是上述配置引起的,您可以尝试以下方法:
RewriteEngine On
# add a trailing slash to all directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ / [L,R=301]
## redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
在处理 Gerfried 提案后,这里是我使用的解决方案。
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
它适用于以下情况:
- 请求目录 -> https 重定向
- 请求现有文件
-> 提供文件
- 请求缺少资源 -> http 404 错误。
如 the mod_rewrite documentation 中所述,应使用 %{LA-U:REQUEST_FILENAME}
形式获取目录名称;这种形式不受所有 <Location>
、mod_rewrite 等路径转换的影响。所以:
RewriteEngine On
RewriteCond %{LA-U:REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
我 运行 一个位于 https 门户后面的 apache 服务器并且目录重定向有问题。 我的 apache 服务器是一个 docker 容器,它接收由 https 门户容器 (steveltn/https-portal) 转发的请求。 一切正常,除了对 http 而不是 https 进行的默认 http 重定向。例如,假设我们在我的 apache 网站中有一个名为 test 的目录。呼叫 https://example.com/test returns a 301 code with redirection to http://example.com/test/。 正确的行为是重定向到 https。
我首先认为这是我的 https 门户配置错误,并询问了 steveltn/https-portal 团队。但他们回答说这是我的 apache 配置问题 (https://github.com/SteveLTN/https-portal/issues/67#issuecomment-257934618)。 答案摘要是
PORTAL does tell the Apache about its existence by the request header X-Forwarded-Proto: https. Some web apps recognize this header automatically, such as WordPress. I guess now it up to you to configure your web app to recognize this header
我尝试了很多在 Internet 上找到的配置,例如这个,但是 none 解决了问题:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
如果我将 RewriteCond 更改为 %{HTTP:X-Forwarded-Proto} https,我会得到一个无限重定向循环。
有什么想法吗?
可能是您的 .htaccess 文件导致了此重定向?
如果问题确实是上述配置引起的,您可以尝试以下方法:
RewriteEngine On
# add a trailing slash to all directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ / [L,R=301]
## redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
在处理 Gerfried 提案后,这里是我使用的解决方案。
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
它适用于以下情况:
- 请求目录 -> https 重定向
- 请求现有文件 -> 提供文件
- 请求缺少资源 -> http 404 错误。
如 the mod_rewrite documentation 中所述,应使用 %{LA-U:REQUEST_FILENAME}
形式获取目录名称;这种形式不受所有 <Location>
、mod_rewrite 等路径转换的影响。所以:
RewriteEngine On RewriteCond %{LA-U:REQUEST_FILENAME} -d RewriteCond %{REQUEST_URI} !(.*)/$ RewriteCond %{HTTP:X-Forwarded-Proto} https RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]