【问题标题】:Dynamic domain redirection to HTTPS and non-www动态域重定向到 HTTPS 和非 www
【发布时间】:2023-01-26 20:32:49
【问题描述】:

我现在有两个域:example.pl 和 example.com。

我试图确保它们都分别转到https://example.plhttp://example.com,但我遇到了问题(或者我的浏览器还记得旧状态)。

我现在有这段代码:


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

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

但我不确定它是否正常工作(http://example.com 以某种方式重定向到https://example.pl)以及这是否可以在一个代码块中完成?

【问题讨论】:

    标签: .htaccess


    【解决方案1】:
    RewriteCond %{HTTP_HOST} ^www. [NC]
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    

    这两个条件都不会成功,因此如果通过 HTTPS 请求 www 子域,规则最终会失败(什么也不做)。

    如果它只是两个域,那么您需要修改第三个条件以允许可选的 www 子域并使用正则表达式交替匹配两个域。

    例如:

    RewriteCond %{ENV:HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www.)?(example.com|example.pl) [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    

    %1 反向引用与最后匹配的 RewriteCond 指令中捕获的子模式匹配,即。 example.comexample.pl。重要的一点是第三个条件总是成功的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多