.htaccess www, non-www, subdomains & vice-versa

Redirect example.com to http://www.example.com (ignore *.example.com, focus on http://www.example.com)

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect http://www.example.com to example.com (ignore *.example.com, this should only redirect www.example.com)

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

Leave a reply