html - 页面未正确重定向错误 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]
解决方案
您必须添加一个测试,因为此时您为每个请求运行第一条规则。
并且仅在 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]
推荐阅读
- bash - Shell - 读取时跳过匹配行
- html - 如何在 xpath 选择中转义符号 (normalize-space())
- html - 如何在 Flask 应用程序中包含 POST 请求中的值?
- c++ - 如何在 Qt creator 中正确链接外部 DLL
- node.js - docker swarm 服务中的nodejs应用,更新没有效果
- c# - C# 跳过等待运算符以允许在 Web API 中执行当前方法
- javascript - 操作多个复选框选择代码如何工作?
- javascript - 通过 srcdoc 更新后 iframe 删除 CSS - 如何使添加的 CSS 持续存在
- matlab - 如何在 MATLAB/OCTAVE 上实现和修改牛顿法?
- amazon-web-services - LEX 服务中的监控会保留多少天的日志?