How to Redirect Subdomains to Other Domain

Posted by Codex73 on Stack Overflow See other posts from Stack Overflow or by Codex73
Published on 2010-06-03T23:48:22Z Indexed on 2010/06/13 11:32 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

What I'm trying to accomplish with htaccess mod-rewrite:

Redirect all sub-domains to new domain name w rewrite rule.

e.g.

test1.olddomain.com ===> test1.newdomain.com

test2.olddomain.com ===> test2.newdomain.com

test3.olddomain.com ===> test3.newdomain.com

This is what I have so far which of course is wrong:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/$1 [R=301,L]

RewriteRule [a-zA-Z]+\.olddomain.com$ http://$1.newdomain.com/ [R=301,L]

Since I'm not a Regular Expression junkie just yet, I need your help... Thanks for any help you can give here. I know also we can compile these first two conditions into one.

Note: The reason I don't redirect all domain using DNS is that a lot of directories need special rewrite rules in order to maintain positions on SEO.

© Stack Overflow or respective owner

Related posts about regex

Related posts about apache