URL 后面的双斜杠

Double Slash behind URL

我将所有非 www 和非 SSL 连接转发到 https://www.*,并在我的 .htaccess 中使用以下代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.tld/ [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/ [R=301,NE,L]

否则:

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.tld%{REQUEST_URI} [R=301,NE,L]