简体   繁体   English

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

[英].htaccess 301 redirect one subdomain to another, for multiple TLDs

I want to change a subdomain of my site and would like to make the RewriteRules I use to 301 users more simple, as currently I have to have multiple rules to cover various eventualities. 我想更改站点的子域,并希望使对301个用户使用的RewriteRules更简单,因为当前我必须有多个规则来解决各种可能的情况。

Is there a way that with one rule I can do the following: 有没有一种方法可以用一条规则执行以下操作:

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

So basically, I need to detect the presence of subold in the host, change this to subnew and redirect to this new subdomain, preserving whichever TLD the user tried to access. 因此,基本上,我需要检测主机中存在subold ,将其更改为subnew并重定向到此新的子域,以保留用户尝试访问的任何TLD。

At the minute, my rules are as follows: 目前,我的规则如下:

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]

Assuming there is just a difference of subold vs subnew in your real domain names. 假设您的真实域名中存在subold vs subnew的区别。

You can have just one rule like this: 您只能有一个这样的规则:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM