【问题标题】:Mod-Rewrite/RewriteRule: Enforce https://www as protocol and domainMod-Rewrite/RewriteRule:强制 https://www 作为协议和域
【发布时间】:2017-02-08 08:02:07
【问题描述】:

我从 stackoverflow (Enforce www and trailing slash with mod_rewrite htaccess on a multi-domain site) 的一个问题中提取了以下代码,并直接从 apache.org 提取。

场景包含三个要求:

  1. 确保生产域始终以 www 开头。
  2. 不要附加 www。到 beta.domain.tld、dev.domain.tld、mobile.domain.tld
  3. 最后确保每个 url 都被重写为 https。

我想出了以下条件:

# Enforce www, if no subdomain is given
RewriteCond %{HTTP_HOST} !^(beta|dev|mobile|www)\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI}/ [R]

# Enfore SSL for all Domains
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

上述规则按预期工作,尽管一个案例不起作用

https://domain.tld/ 未正确重定向到 https://www.domain.tld/

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    尝试用这个替换你的 2 代码:

    RewriteCond %{HTTP_HOST} !^(?:beta|dev|mobile|www)\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
    
    RewriteCond %{HTTPS} =off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 2012-11-18
      • 2019-12-17
      • 1970-01-01
      • 2014-01-20
      • 2015-07-18
      • 2016-06-15
      相关资源
      最近更新 更多