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
Related videos on Youtube
Comments
-
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 about 9 yearsGood point. Hope it doesn't confuse anyone. And thanks for the comment.
-
Joseph about 9 yearsI fixed the terminology.
-
-
Joseph about 9 yearsNot working even with those changes. I get a 404 error since the document root does not currently exist.
-
krisFR about 9 years@Joseph Of course. You now need to setup a
DocumentRoot
for*.domain2.com
. If you want the same you have fordomain1.com
, adding this should workServerAlias *.domain1.com *.domain2.com
-
Joseph about 9 yearsThe 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 about 9 yearsWill the ServerAlias like you have it actually cause a redirect?
-
serverliving.com about 9 yearsYes. you need to have ServerAlias *.domain2.com so the rewrite rules will be applied to all subdomains.
-
MrWhite about 5 yearsThis 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 about 5 yearsNote that this makes the subdomain optional, so it will also redirect the domain apex (ie.
domain1.com
todomain2.com
). (Don't forget to escape the literal dots in regex.)