Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

all subdomains go to non .htaccess

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
Avatar of Dr. Klahn
Dr. Klahn

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

Avatar of rgb192

ASKER

so every subdomain must  be written?
ASKER CERTIFIED SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks