我将任何非 www 和非 SSL 连接转发到 https://www.*,并在我的 .htaccess 中使用此代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L]
但是当我尝试将这个确切的代码放入我的 000-default.conf 和 000-default-le-ssl.conf 时,它会放置一个双斜杠 (https://www.domain.tld**//* *) 在 URL 后面。为什么?
最佳答案
But when I try to put this exact code in my 000-default.conf and 000-default-le-ssl.conf, it puts a double slash (
https://www.domain.tld//
) behind the URL. Why?
这是因为在 http 服务器配置中,前导斜杠在 RewriteRule
中也与 .htaccess 文件不同。
您可以使用此规则使其在两种类型的文件中表现相同:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*)$ https://www.domain.tld/$1 [R=301,NE,L]
否则:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,NE,L]
关于apache - URL 后面的双斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46604130/