【问题标题】:.htaccess: HTTP to HTTPS redirecting not working.htaccess:HTTP 到 HTTPS 重定向不起作用
【发布时间】:2015-07-15 12:49:28
【问题描述】:

我想重定向: 1) http://example.comhttps://www.example.com 2) http://www.example.comhttps://www.example.com

但我的 .htaccess 通常会忽略 RewriteCond 中的 %{HTTPS}... 因此我无法处理提到的第二种情况,它给了我循环重定向错误消息。

.htaccess如下:

# Redirect non-www urls to www
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example\.com/?$ 
RewriteRule ^(.*)$ https://www.example.com [NC,R=301]

RewriteCond %{HTTP_HOST} ^example\.com/?$ 
RewriteRule ^(.*)$ https://www.example.com [NC,R=301]

RewriteCond %{HTTP_HOST} ^example\.com\?.*$
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301]

我会尽量说非常好的谢谢!任何能帮助我解决这个问题的人。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    你真的只需要一条规则就可以满足你的需求。

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC,OR] 
    RewriteCond %{HTTPS} !^on
    RewriteRule ^(.*)$ https://www.example.com [NC,R=301,L]
    

    【讨论】:

    • 获得循环重定向 :(
    • 清除浏览器缓存或在其他浏览器中尝试。该规则有效。
    猜你喜欢
    • 2014-03-10
    • 2018-06-11
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多