1

I am looking to change a root domain of a site.

The domain in question has several sub domains as aliases.

I would like to use htaccess to redirect the sub domains to the new domain:

eg :

test.domain1.com > test.domain2.com

Looking around I can see that the condition I need is as follows :

RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$ [NC]

But I cannot see what the RewriteRule should be.

Any help would be greatly appreciated.

Amit Verma
  • 39,545
  • 18
  • 87
  • 107
n00bstacker
  • 323
  • 1
  • 6
  • 23

2 Answers2

3

To redirect test.subdomain.com to test.subdomain2.com you can use the following Rule :

RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.com$ [NC]
RewriteRule ^ http://%1.domin2.com%{REQUEST_URI} [NE,L,R]
Amit Verma
  • 39,545
  • 18
  • 87
  • 107
2

Here's the correct way to do it:

Remember: Replace example.com, and example\.com below, with your own domain name.

For HTTPS:// protocol:

RewriteCond %{HTTP_HOST} ^(([^.]+)\.)?example\.com$ [NC]
RewriteRule ^ https://%1example.com%{REQUEST_URI} [NE,R=301,L]

For HTTP:// protocol:

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

@starkeen's answer doesn't account for domains with no sub-domain at all (e.g. simply, 'https://example.com').

Pang
  • 9,073
  • 146
  • 84
  • 117
James Anderson Jr.
  • 711
  • 1
  • 7
  • 23