【问题标题】:Redirect from HTTP to HTTPS preserving old redirections with apache从 HTTP 重定向到 HTTPS,保留使用 apache 的旧重定向
【发布时间】:2016-10-26 14:22:59
【问题描述】:

我需要使用 apache 服务器将所有流量从 http 重定向到 https。我想保留我的实际重定向(不带 www -> 带 www)

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

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

有了这个 htaccess,我有 http->https 重定向,没有 www->有 www 重定向,但不要同时进行。

我需要这个重定向:

http://domain.com -> https://www.domain.com

http://domain.com/foo/bar -> https://www.domain.com/foo/bar

谢谢

【问题讨论】:

    标签: apache .htaccess redirect


    【解决方案1】:

    您可以使用[OR] 子句将这两个条件组合到一个重定向规则中:

    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
    

    现在,如果您想避免在此规则中对域名进行硬编码,请使用:

    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
    

    在测试此更改之前清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2016-01-17
      • 2018-04-09
      • 2018-02-28
      • 2013-09-27
      • 2016-02-04
      • 1970-01-01
      • 2019-11-08
      • 2018-08-25
      相关资源
      最近更新 更多