Forum Moderators: phranque
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I suspect you will find that the server configuration does not 'point' requests for all subddomains to the same directory as for "www" and "non-www". This may be something set up in httpd.conf or conf.d -- either manually, or using a "control panel."
In order for your .htaccess code to have any effect, requests must 'pass through it' in traversing the directory path to any files being accessed. So if the other subdomains are not 'pointed' to the directory in which your .htaccess file resides, it can have no effect.
Examining the server configuration should clarify this: If the additional subdomains are declared in a separate <VirtualHost> container and have a different <DocumentRoot> path, then your .htaccess code won't affect them.
The solution if this is the case is either to reconfigure the server DocumentRoot settings so that all requests pass through your top-level .htaccess file, or to reproduce the code in the DocumentRoot-defined directories for your additional subdomains.
I said the code looks "OK" because you don't need "[NC]" on the RewriteCond, since you want *all* non-canonical requests to be redirected. Using "[NC]" there means that case-variations of the canonical doamin *won't* be redirected, and that's "non-optimal."
Jim