.htaccess从http和www到https非www

时间:2017-09-22 10:42:45

标签: .htaccess

在我的网站网址的四种可能变体中,我想重定向到https://example.com(https:// non-www)。

我的.htaccess文件包含以下内容:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这会从http://重定向到https://,但不会从www重定向到非www

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:1)

让你的第一条规则像这样通用:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1 [L,R=301,NE]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} !on
RewriteRule ^(.*?)/?$ https://%1/$1 [L,R=301,NE]

确保在测试此更改之前清除浏览器缓存。

相关问题