all subdomains go to non .htaccess

rgb192
rgb192 used Ask the Experts™
on
linux shared server
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] 

Open in new window


this is .htaccess code (i hope it is correct)
to have www. redirect to non www

sometimes I type wwwww.example.com or ww.example.com
could I have a rule where all subdomains forward to http://example.com
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dr. KlahnPrincipal Software Engineer

Commented:
Two methods.  The first assumes that any prefix that is not "www" or "null" is a subdomain.  The second requires explicitly listing subdomains as conditions.

Note that neither of these methods preserve query strings.  If the site does not use query strings then this is no issue.  If the site uses query strings then %{QUERY_STRING} must be appended to the target URL.

Now here is the catch.  In order for this to work, the server must be either (a) non-vhost and accept any domain name at all, or (b) a vhost with the possible subdomain names explicitly accepted in the vhost definition.  Otherwise the server will reject the request as "not for this site."


RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule .* http://example\.com%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^subdomainfirst\.example\.com [NC,OR]
(continue as required for all subdomains)
RewriteCond %{HTTP_HOST} ^subdomainlast\.example\.com [NC]
RewriteRule .* http://example\.com%{REQUEST_URI}

Open in new window

Author

Commented:
so every subdomain must  be written?
Principal Software Engineer
Commented:
Since we don't know how your server is set up, that's a question we can't answer specifically.  But per above, if the server is a vhost, then every possible subdomain must be both listed in the rules and called out as a subdomain in the vhost's alias definitions.

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial