错误的重定向从http到https

时间:2016-11-24 18:37:16

标签: apache .htaccess mod-rewrite https http-redirect

尝试使用https,但当我在.htaccess中使用以下内容时出现重定向问题错误

这是我.htaccess

中的内容
RewriteEngine On
 RewriteBase /

 RewriteCond %{HTTP} on [OR]
 RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

 #Uncomment lines below when uploading to the live server
 RewriteCond %{HTTP_HOST} !^(www\.)example\.com$ [NC]
 RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-l
 RewriteRule ^.*$ index.php [QSA,L,NC]

 #Uncomment lines below when uploading to the live server
 RewriteCond %{HTTP_REFERER} !^$
 RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com/.*$ [NC]
 RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]

请帮忙

1 个答案:

答案 0 :(得分:1)

您的第一条规则有许多错误的[OR]条件,这会导致重定向循环。

将您的第一条规则更改为:

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]

清除浏览器缓存后进行测试。