Forum Moderators: phranque
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
I have the server set up as a virtual host with ServerAlias. Is that making it not work?
Is there anything in your server error log?
Is UseCanonicalName set to "on" with the "www" hostname declared as the (incorrect) canonical ServerName?
Are there any other RewriteRules or mod_alias directives which may be interfering?
BTW, don't end-anchor the pattern used to check the hostname. Or if you do feel you need to end-anchor it, then follow the ".com" with \.?(:[0-9]+)?$ so that FQDNs and hostnames with appended port numbers won't defeat the rule. Also, escape all literal periods in regex patterns by preceding them with "\":
RewriteCond %{HTTP_HOST} ^www\.example\.com\.?(:[0-9]+)?$ [NC] An alternative, if you don't plan to use subdomains in additional to "www", would be to redirect anything that is *not* exactly the canonical hostname:
RewriteCond %{HTTP_HOST} !^example\.com$ (Note negated pattern, no FQDN or port allowed, no uppercase allowed -- all would get redirected to all-lowercase "example.com")
Jim