将https和http www重定向到https非www

时间:2014-06-05 08:06:06

标签: apache .htaccess url-rewriting

我想重写以下内容

http://example.com
http://www.example.com
https://www.example.com

https://example.com

尝试使用

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

似乎无法正常工作

2 个答案:

答案 0 :(得分:0)

您可以使用:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]

答案 1 :(得分:0)

我说最容易做的就是写两条规则:

# First make sure HTTPS is being used
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Then make sure www isn't being used
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]