【问题标题】:Force HTTPS & Non-WWW for a subdomain dynamically动态强制子域使用 HTTPS 和非 WWW
【发布时间】:2017-01-09 15:32:20
【问题描述】:

我正在尝试制定一个 .htaccess 规则,我可以在我的主域和子域上使用该规则,而无需更改每个域的代码。

我已经为子域尝试了以下方法来尝试强制使用 https 和非 www,但它无法正常工作。

<IfModule mod_rewrite.c>
    # Force HTTPS & NON-WWW
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www?
    RewriteCond %{HTTPS} !=on  [OR]
    RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC]
    RewriteRule ^ https://test\.website\.com%{REQUEST_URI} [R=301,L]
</IfModule>

目前,如果我要去http://www.test.website.com/test/,它会重定向到https://test%2Cwebsite.com/,所以它有点工作但不完全。

我也一直在尝试更动态地使用:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} !=on  [NC]
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

但这根本不起作用。实现这一目标的最佳解决方案是什么?

它本质上应该强制执行以下所有操作:

http://website.com/                   => https://website.com/
http://www.website.com/               => https://website.com/
https://www.website.com/              => https://website.com/

http://website.com/testing/           => https://website.com/testing/
http://www.website.com/testing/       => https://website.com/testing/
https://www.website.com/testing/      => https://website.com/testing/

http://test.website.com/              => https://test.website.com/
http://www.test.website.com/          => https://test.website.com/
https://www.test.website.com/         => https://test.website.com/

http://test.website.com/testing/      => https://test.website.com/testing/
http://www.test.website.com/testing/  => https://test.website.com/testing/
https://www.test.website.com/testing/ => https://test.website.com/testing/

【问题讨论】:

  • "它重定向到https://test%2Cwebsite.com/" - 那里发生了一些奇怪的事情,它与您在上面发布的指令无关。 %2C 是编码逗号?!这是从哪里来的?
  • @w3dk 我也看不懂 %2C,网址中没有逗号,但它正在放一个。

标签: regex apache .htaccess mod-rewrite


【解决方案1】:

您可以使用这个单一的动态规则来实现所有http -&gt; httpswww -&gt; non-www 重定向:

RewriteEngine On

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

确保此规则位于顶部,并在测试此更改之前清除浏览器缓存。

【讨论】:

  • 第三个RewriteCond 指令是否必要?如果您重新排序前两个并包含捕获组,那么第一条规则是什么:^www\.(.+)
  • @w3dk 是的,必须捕获可选www 之后的部分。之前的 2 个条件是使用 OR 子句,因此只会执行一个,我们无法从中捕获任何内容。
  • 它在我的初始测试中完美运行,只有 1 条重写规则是完美的。
【解决方案2】:

试试这个:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # First, force the HTTPS, whatever it is:
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Then drop the www, if any:
    RewriteCond %{HTTP_HOST} ^www\.(.*)
    RewriteRule .* https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
## Results:
# http://website.com/      => https://website.com/
# http://www.website.com/  => https://website.com/
# https://www.website.com/ => https://website.com/

# http://website.com/testing/      => https://website.com/testing/
# http://www.website.com/testing/  => https://website.com/testing/
# https://www.website.com/testing/ => https://website.com/testing/

# http://test.website.com/      => https://test.website.com/
# http://www.test.website.com/  => https://test.website.com/
# https://www.test.website.com/ => https://test.website.com/

# http://test.website.com/testing/      => https://test.website.com/testing/
# http://www.test.website.com/testing/  => https://test.website.com/testing/
# https://www.test.website.com/testing/ => https://test.website.com/testing/

【讨论】:

    【解决方案3】:

    您的重写规则必须是(在第一个示例中):

    RewriteRule ^/(.*) https://test.website.com/$1 [R=301,L]
    

    删除反斜杠,因为重写目标不是正则表达式。

    对于您的整体解决方案,我想您需要两个单独的重写。一个进行https检查,另一个删除www。我想一次重写是不可能的,因为你需要检查两个条件,其中只有一个可能匹配。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-20
      • 2023-03-23
      • 2015-07-18
      • 2016-03-20
      • 2021-04-12
      • 2023-01-26
      • 2016-06-02
      • 1970-01-01
      相关资源
      最近更新 更多