【问题标题】:htaccess condition that adds www. to the url only if it doesn't already contain a string in the url添加 www 的 htaccess 条件。仅当它在 url 中不包含字符串时才到 url
【发布时间】:2019-05-31 04:00:52
【问题描述】:

我有一个网站:example.com。

我有一个开发站点:development.example.com

我被要求在前面加上“www”。到网址,但我无法将其添加到开发站点。

有没有办法获得条件来检查该网站没有“www.”并且不包含字符串“development.”。如果是这种情况,请添加“www”。字符串到网址?

这是我现在的:

RewriteEngine On

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

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

我的逻辑有点解释:如果我先运行 https 重定向,我可以确保两个 'www.' 的 url。和“发展”。已移至 https。 然后我希望编辑'!^www。在第二部分添加“www”的条件。如果没有“www”。并且没有“发展”。

但是这个当前规则显然将我的开发站点的 url 更改为 https://www.development.example.com 导致问题。

答案是否类似于:

RewriteCond %{HTTP_HOST} !^www\..!^development\. [NC]

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    我设法将两个 RewriteCond 链接在一起的方式是这样的:

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

    进一步阅读后 - 您可以在 RewriteRule 之前链接您的条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多