【问题标题】:Forcing HTTPS doesn't work强制 HTTPS 不起作用
【发布时间】:2017-03-01 16:36:23
【问题描述】:

我正在尝试通过 .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

【问题讨论】:

    标签: .htaccess https


    【解决方案1】:

    您的第一个重写规则是罪魁祸首,对于每次成功重定向到 https,它会在 external 重定向...

    试试这个稍微修改过的版本:

    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]
    

    【讨论】:

    • 我刚刚尝试了您的解决方案,但仍然遇到同样的错误。
    • 我建议您清除浏览器缓存或进行深度重新加载...如果错误仍然存​​在,那么您要么必须启用重写日志记录(在重写模块文档中找到详细信息),它会准确地告诉您重写引擎内部发生了什么,或者您使用网络嗅探器检查发送的实际重定向。
    • 哦,我只是看到你必须根据RedirectRule ^/blogs$ %{REQUEST_SCHEME}://patrickwhitehouse.pw/blog.html调整你的这条规则RedirectMatch ^/blogs$ http://www.patrickwhitehouse.pw/blog.html
    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 2016-05-24
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多