johnsona johnsona - 10 months ago 51
Apache Configuration Question

Need to redirect dynamic subdomain to new main domain

Currently I have a domain with the format of
, where
is a variable. I want to redirect all of the possible subdomains to
so that if someone goes to
they get redirected to

So far I have tried

RewriteCond %{HTTP_HOST} ([.*])$
RewriteRule (.*)$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?


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

RewriteEngine On

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