.htaccess 301将一个子域重定向到另一个子域,用于多个TLD

时间:2013-09-20 12:17:46

标签: regex apache .htaccess redirect mod-rewrite

我想更改我网站的子域名,并希望将我用于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]

1 个答案:

答案 0 :(得分:2)

假设您的真实域名中存在subold vs subnew的差异。

你可以只有一个这样的规则:

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