Redirect wildcard subdomain to same subdomain on different domain

5,256

Solution 1

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]

Solution 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

Share:
5,256

Related videos on Youtube

Joseph
Author by

Joseph

PHP Developer located in SW Houston.

Updated on September 18, 2022

Comments

  • Joseph
    Joseph almost 2 years

    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
      Joseph about 9 years
      Good point. Hope it doesn't confuse anyone. And thanks for the comment.
    • Joseph
      Joseph about 9 years
      I fixed the terminology.
  • Joseph
    Joseph about 9 years
    Not working even with those changes. I get a 404 error since the document root does not currently exist.
  • krisFR
    krisFR about 9 years
    @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
  • Joseph
    Joseph about 9 years
    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
    Joseph about 9 years
    Will the ServerAlias like you have it actually cause a redirect?
  • serverliving.com
    serverliving.com about 9 years
    Yes. you need to have ServerAlias *.domain2.com so the rewrite rules will be applied to all subdomains.
  • MrWhite
    MrWhite about 5 years
    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
    MrWhite about 5 years
    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.)