我正在尝试将我的网址重定向到https。我想做以下事情:
http://example.com => https://example.com
http://www.example.com => https://example.com
www.example.com => https://example.com
example.com => https://example.com
所以将每个网址转换为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]
在第一个RewriteRule之前和之后,但它会产生无限循环。 有人能帮助我吗?
答案 0 :(得分: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]
在测试之前清除浏览器缓存。
答案 1 :(得分:0)
我在这里找到了解决方案:
### Force HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]