johnsona johnsona - 1 year ago 96
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?

Answer Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download