【问题标题】:.htaccess 301 redirect one subdomain to another, for multiple TLDs.htaccess 301 将一个子域重定向到另一个子域,用于多个 TLD
【发布时间】:2013-09-25 19:59:15
【问题描述】:

我想更改我网站的一个子域,并想让我对 301 用户使用的 RewriteRules 更简单,因为目前我必须有多个规则来涵盖各种可能性。

有没有一种方法可以通过一条规则执行以下操作:

subold.domain.com -> subnew.domain.com
subold.domain.co.uk -> subnew.domain.co.uk
subold.domain.local -> subnew.domain.local
subold-staging.domain.com -> subnew-staging.domain.com
subold-staging.domain.co.uk -> subnew-staging.domain.co.uk
subold-staging.domain.local -> subnew-staging.domain.local

所以基本上,我需要检测主机中是否存在subold,将其更改为subnew 并重定向到这个新的子域,保留用户尝试访问的任何TLD。

此刻,我的规则如下:

RewriteCond %{HTTP_HOST} ^subold.domain.local [NC]
RewriteRule ^(.*)$ http://subnew.domain.local/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^subold-staging.domain.local [NC]
RewriteRule ^(.*)$ http://subnew-staging.domain.local/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^subold.domain.com [NC]
RewriteRule ^(.*)$ http://subnew.domain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^subold-staging.domain.com [NC]
RewriteRule ^(.*)$ http://subnew-staging.domain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^subold.domain.co.uk [NC]
RewriteRule ^(.*)$ http://subnew.domain.co.uk/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^subold-staging.domain.co.uk [NC]
RewriteRule ^(.*)$ http://subnew-staging.domain.co.uk/$1 [L,R=301]

【问题讨论】:

    标签: regex apache .htaccess redirect mod-rewrite


    【解决方案1】:

    假设您的真实域名中只有subold vs subnew 的差异。

    你只能有一个这样的规则:

    RewriteCond %{HTTP_HOST} ^subold\.(.+)$ [NC]
    RewriteRule ^ http://subnew.%1%{REQUEST_URI} [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2014-03-16
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      相关资源
      最近更新 更多