2016-12-02 53 views
1

我正在尝试将我的网址重定向到https。我想做到以下几点:.htaccess:重定向到https,没有www结果循环

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

所以每个URL转换为https://example.com(随着去除WWW)!

我目前的.htaccess看起来是这样的:

RewriteEngine On 
RewriteBase/

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

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.ogg|\.wav|\.mp3|\.mp4|\.zip|\.pdf|\.fav|\.rar|\.doc)$ [NC] 
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA] 

我都试过之后和第一个重写规则之前添加

RewriteCond %{HTTPS} off [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

,但它会导致死循环。 有人可以帮我吗?

回答

1

您的第一条规则改成这样:

# remove www and turn on https in same rule 
RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE] 

而且在测试前清除浏览器缓存。

+0

我不知道为什么,但它仍然不工作。不知何故,它会导致无限循环:/ – user3681084

+0

要测试注释掉所有其他规则,并确保您完全清除浏览器缓存。 – anubhava

+0

没有工作要么 – user3681084

相关问题