首页 > 解决方案 > 页面未正确重定向错误 http - https 自动重定向在网站中显示错误

问题描述

每当我输入以下 http - https 自动重定向代码时,我都会收到此错误“页面未正确重定向

连接到“网站 URL”时出错。这个问题有时可能是由禁用或拒绝接受 cookie 引起的。”

htaccess 代码如下。

 # Rewrite engine code https redirect
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

 # Rewrite engine code for www to non ww version of website
 RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
 RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]

标签: html.htaccesshttps

解决方案


您必须添加一个测试,因为此时您为每个请求运行第一条规则。
并且仅在 https 之前不带 www 重定向。

尝试:

RewriteEngine on
# http(s)://(www.)?domain   ->  https://domain
# www -> https without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

推荐阅读