.htaccess - http:// 到 https://www 重定向不起作用
问题描述
我想重定向http://
到https://www
. https://
现在它正在重定向https://www
我正在使用 siteground 我尝试了 StackOverflow 上给出的许多答案。
我试过的代码示例:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
解决方案
你很接近你在检查 https 条件后不需要使用OR
条件,你能试试看吗?以下规则应将 https 应用于http
带有/不带有www
.
请确保在测试 URL 之前清除浏览器缓存。
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
推荐阅读
- android - 为什么触发 Firebase onDataChange() 方法后,我的 Google Map 会不断创建新的地图实例?
- php - 如何使用php函数将mysql查询输出到csv更改列值
- angular - 如何使用拦截器添加 api_key
- python - 无法使用 pylint 导入“app.xxx”(vscode、virtualenv)
- javascript - 如何在反应js中换行后使文本对齐
- c# - 从 Azure Dev Ops 向 Azure Web App Service 进行身份验证
- java - 单击图像时不出现对话框片段
- excel - 如何将我的 Excel DocumentProperty 对象变量(位于 ByRef 函数中)设置为 Word BuiltinDocumentProperties 集合项?
- javascript - 正则表达式 - 查找除某些模式之外的所有唯一字符串
- html - 使用 float:right 时文本放置不正确