【问题标题】:Using HTTP_HOST in .htaccess file在 .htaccess 文件中使用 HTTP_HOST
【发布时间】:2020-09-23 06:53:43
【问题描述】:

我目前正在使用它通过 htaccess 强制 https 和 www:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]

现在我想更改R=301 中的R=302,并放弃域名本身。我不希望 example.com 出现在其中的原因是,将来某些国家/地区的域名可能会有所不同。

所以我在想

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

...但这会将www.example.com 重定向到www.www.example.com 对吗? 在这种特定情况下如何正确执行此操作?我仍然想在一个重定向中强制使用 www 和 https。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    我仍然想在一个重定向中强制使用 www 和 https。

    你需要像这样使用这个重定向规则来避免硬编码:

    # add www and turn on https in same rule
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多