强制HTTPS不起作用

时间:2017-03-01 16:36:24

标签: .htaccess https

我试图通过.htaccess强制使用HTTPS,并且我经常收到太多重定向错误。

这是我的.htaccess文件:



RewriteEngine On

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

#force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Options +MultiViews




<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^&\ ]+).html
RewriteRule .* /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [L]

RedirectMatch ^/blogs$ http://www.patrickwhitehouse.pw/blog.html
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您的第一个重写规则是罪魁祸首,对于每次成功重定向到https,它会在下一个请求之后再次重定向到硬编码http < em>外部重定向...

请尝试使用稍微修改过的版本:

RewriteEngine On    

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

#force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

或者,有点清理:

RewriteEngine On    

# force host 'patrickwhitehouse.pw'
RewriteCond %{HTTP_HOST} !^patrickwhitehouse.pw$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://patrickwhitehouse.pw%{REQUEST_URI} [L,R=301]

# force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]