Redirect wildcard subdomain to same subdomain on different domain

apache-2.2mod-aliasmod-rewriterewritewildcard-subdomain

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>

Best Answer

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]
Related Topic