htaccess www to non-www with subdomain
This question comes close but implementing with a subdomain doesn't work.


It should work as long as you modify the rewrite rule to account for the subdomain:

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


The code you posted was missing the ^ before foo\.bar\.com on the RewriteCond line. The purpose of that conditional is supposed to be to catch incoming requests where the domain isn’t exactly “foo.bar.com” (so that it can redirect them to the correct domain on the following line). However without that ^, the conditional would only catch requests where the domain doesn’t end with “foo.bar.com”.  But “www.foo.bar.com” does in fact end with “foo.bar.com” so it’s not going to get caught.

Hope that makes sense.

Update:

Here’s a hostname-agnostic version that’s a little easier on the eyes:

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