首页 > 解决方案 > 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>

标签: .htaccessredirectmod-rewritehttpsurl-rewriting

解决方案


你很接近你在检查 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]

推荐阅读