发现http到https双重定向

问题描述 投票:1回答:1

我试图通过三个选项将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>
.htaccess redirect http-status-code-301 http-redirect
1个回答
1
投票

您有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]

确保使用此规则替换两个规则并使用新的浏览器进行测试以避免旧的浏览器缓存。

© www.soinside.com 2019 - 2024. All rights reserved.