Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing www with htaccess file for subdomains

Tags:

.htaccess

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

like image 409
Mickey Slater Avatar asked Oct 25 '11 19:10

Mickey Slater


1 Answers

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.

like image 101
Seybsen Avatar answered Oct 19 '22 14:10

Seybsen