2

I have a domain with dynamic subdomains. The domain, unfortunately, has changed. As such I need to redirect

*.domain1.com

to

*.domain2.com

while keeping the subdomains the same. I have looked through the mod_alias and mod_rewrite documentation as well as examples from all over Google, but have found no information regarding keeping the subdomain the same when it is dynamic.

I have full control over the server, so I am looking at using the VirtualHost httpd.conf settings, but am not sure if that is the best route to go.

Any suggestions on where to look is much appreciated.

The current conf values are:

<VirtualHost *:80>
    DocumentRoot /var/www/domain1
    ServerName domain1.com
    ServerAlias *.domain1.com
    ErrorLog logs/domain1.com-error_log
    CustomLog logs/domain1.com-access_log common

    # This is my latest attempt
    RewriteCond %{HTTP_HOST} ^(.*)\.domain1\.com$ [NC]
    RewriteRule ^(.*)$ http://%1.domain2.com/$1 [R=301,L]
</VirtualHost>
Joseph
  • 141
  • 1
  • 7

2 Answers2

2

You can try something like below.

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

Let me know if that helps

MrWhite
  • 11,643
  • 4
  • 25
  • 40
serverliving.com
  • 875
  • 6
  • 15
  • Not working even with those changes. I get a 404 error since the document root does not currently exist. – Joseph May 12 '15 at 19:13
  • @Joseph Of course. You now need to setup a `DocumentRoot` for `*.domain2.com`. If you want the same you have for `domain1.com`, adding this should work `ServerAlias *.domain1.com *.domain2.com` – krisFR May 12 '15 at 20:00
  • The DocumentRoot exists for domain2. The DocumentRoot for domain1 does not exist because the folder was moved to be the root folder for domain2. And the redirect for www.domain1.com to www.domain2.com works beautifully. I just can't get the redirect to work with the wildcard subdomains. – Joseph May 12 '15 at 20:02
  • Will the ServerAlias like you have it actually cause a redirect? – Joseph May 12 '15 at 20:03
  • 1
    Yes. you need to have ServerAlias *.domain2.com so the rewrite rules will be applied to all subdomains. – serverliving.com May 13 '15 at 03:10
  • This solution is suited to `.htaccess`, it's not quite correct if used directly in the vHost config. If you use this in vHost then you'll get a double slash in the resulting redirect. This code also makes the subdomain mandatory. It won't redirect the domain apex (ie. `domain1.com`) - if that is an issue (would just need to tweak the regex to include the dot, as in the other answer, if that is required). – MrWhite Jun 18 '19 at 09:06
2

I finally found an answer on StackOverflow by @Marty.

Code reproduced here:

RewriteCond %{HTTP_HOST} ^(.+\.)?domain1.com$ [NC]
RewriteRule ^ http://%1domain2.com%{REQUEST_URI} [R=301,L]
MrWhite
  • 11,643
  • 4
  • 25
  • 40
Joseph
  • 141
  • 1
  • 7
  • Note that this makes the subdomain optional, so it will also redirect the domain apex (ie. `domain1.com` to `domain2.com`). (Don't forget to escape the literal dots in regex.) – MrWhite Jun 18 '19 at 09:09