johnsona johnsona - 2 months ago 11
Apache Configuration Question

Need to redirect dynamic subdomain to new main domain

Currently I have a domain with the format of

subdomain.domain.com/path
, where
subdomain
is a variable. I want to redirect all of the possible subdomains to
subdomain.new_domain.com/path
so that if someone goes to
subdomain_A.domain.com
or
subdomain_B.domain.com
they get redirected to
subdomain_A.new_domain.com
and
subdomain_B.new_domain.com
respectively.

So far I have tried

RewriteCond %{HTTP_HOST} ([.*]).domain.com$
RewriteRule (.*) %1.new_domain.com/$1


to accomplish this but for some reason the regexes will not substitute in the way that I am hoping for.

Can anyone please offer some suggestions or even confirm that this is possible using apache?

Answer

Your regex is not entirely correct. You can use this redirect rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$
RewriteRule ^ http://%1.new_domain.com%{REQUEST_URI} [L,NE,R=301]
Comments