发现 http 到 https 双重重定向

http to https double redirection found

我正在尝试使用三个选项将 http 重定向到 https

但是,第二个选项显示来自 http://www > https://www > https://

的双重重定向

以下是我在 .htaccess 中的代码。双重重定向可以吗?

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

因为您有 2 个重定向规则。重定向可能会发生两次:

  1. 使用第一个规则 http://www.example.comhttps://www.example.com
  2. 使用第二条规则 https://www.example.comhttps://example.com

您可以使用这条规则在单个 301 重定向中完成这两项操作:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

确保将这两个规则都替换为这个规则,并使用新浏览器进行测试以避免旧浏览器缓存。