【问题标题】:.htaccess redirect when one domain is a subset of the other当一个域是另一个域的子集时,.htaccess 重定向
【发布时间】:2017-01-17 05:51:45
【问题描述】:

抱歉,我不知道如何正确使用标题。

我有几个域,我想使用 .htaccess 301 重定向到我的主域。

这就是我正在做的:

RewriteCond %{HTTP_HOST} example.co.za [NC]
RewriteRule ^(.*)$ http://primarydomain.co.za/$1 [L,R=301]

我在将 .co 域重定向到类似的 .co.za 域时遇到问题,例如:

// this causes infinite loop
RewriteCond %{HTTP_HOST} example.co [NC]
RewriteRule ^(.*)$ http://example.co.za/$1 [L,R=301]

我明白为什么会发生这种情况,但我不知道如何编写该规则或其条件。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting url-redirection subdomain


    【解决方案1】:

    使用模式锚点:^$ 分别标记字符串的开头或结尾:

    RewriteCond %{HTTP_HOST} ^example\.co$ [NC]
    RewriteRule ^ http://example.co.za%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 谢谢。很抱歉在这里回复的延迟。我接受了这个答案,因为它向我解释了锚点..
    【解决方案2】:

    这样试试吧,

    RewriteCond %{HTTP_HOST} ^example.co$ [NC]
    RewriteRule ^ http://example.co.za/$1 [L,R=301]
    

    【讨论】:

      【解决方案3】:

      您可以使用否定条件并避免使用正则表达式:

      # if host is not example.co.za
      RewriteCond %{HTTP_HOST} !=example.co.za
      # then redirect to example.co.za
      RewriteRule ^ http://example.co.za%{REQUEST_URI} [NE,L,R=301]
      

      【讨论】:

        猜你喜欢
        • 2016-03-26
        • 2020-05-04
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 2012-05-08
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        相关资源
        最近更新 更多