【问题标题】:Redirect non-www and non-http to https将非 www 和非 http 重定向到 https
【发布时间】:2016-10-30 09:52:07
【问题描述】:

昨天我在服务器上安装了 SSL。因为我无法访问某些页面。

www.example.com/amsterdam/shoes

example.com/amsterdam/

^ 两者都不会重定向到 https://,甚至不会重定向到 http://

www.example.com/amsterdam

^ 确实重定向到 https://

如何通过 .htaccess 使用www 将所有页面重定向到HTTPS

【问题讨论】:

标签: .htaccess redirect mod-rewrite https


【解决方案1】:

用途:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

或者你可以试试:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

【讨论】:

    【解决方案2】:
    RewriteEngine on
    
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R=301,NE]
    

    这会将httpnon-www 重定向到https://www

    【讨论】:

    • 谢谢,这对我有用。你有理由不使用 R=301 或 NE 吗? (我在其他重定向中看到了很多)
    • @Bombelman 抱歉,这是一个错字。我已经用R=301,NE 标志更新了我的答案。谢谢你的评论。干杯!
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多