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
  • 40,709
  • 21
  • 93
  • 115
n00bstacker
  • 323
  • 2
  • 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
  • 40,709
  • 21
  • 93
  • 115
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,564
  • 146
  • 81
  • 122
James Anderson Jr.
  • 760
  • 1
  • 8
  • 26