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
LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dr. KlahnPrincipal Software EngineerCommented:
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

0
rgb192Author Commented:
so every subdomain must  be written?
0
Dr. KlahnPrincipal Software EngineerCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.