Mickey Slater
Mickey Slater

Reputation: 404

removing www with htaccess file for subdomains

I have a variety of sites that are subdomain specific sites. http://sub.domain.com http://apple.domain.com etc.

users occasionally complain that the site is not working and then i find out they went to http://www.sub.domain.com or http://www.apple.domain.com and are met with a server error page of sorts

what kind of htaccess magic do i need to turn http://www.sub.domain.com -> http://sub.domain.com

thanks

*fwiw i did search through previous questions before asking and did not find my answer

Upvotes: 5

Views: 8361

Answers (1)

Seybsen
Seybsen

Reputation: 15587

If the VHost is really pointing to the same docroot for www.sub.domain.com and sub.domain.com, you can place a .htaccess-file with following content in the doc-root:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
RewriteRule (.*) http://sub.domain.com$1 [R=301,L]

That will redirect all domains which are pointing to this docroot to sub.domain.com

EDIT:

For multiple Subdomains in one single .htaccess-file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com$1 [R=301,L]

This is untested from top of the head.

Upvotes: 14

Related Questions