2016-11-24 71 views
1

试图去HTTPS,但是当我用下面我.htaccess我得到重定向问题错误从http不正确重定向到HTTPS

这里是我在我的.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] 

请帮助

+0

如果可以的话,请考虑你的方法来定义重定向,如果你可以避免使用.htaccess,并且更好地在virtualhsot中定义它的大部分。大多数情况下,重定向到ssl涉及定义合适的虚拟主机和简单的Redirect/https://主机名/,request_filename检查然后转到index.php,都可以删除单个Fallbackresource指令等等。 –

回答

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] 

和测试清除浏览器缓存后。

+1

谢谢,它看起来像解决了我的问题。 – AlexB