Alex Rivera | Logout

How to always remove WWW from a url with mod_rewrite?

Asked 2010-03-02T07:28:26.880
20

I'm using the following to try and remove WWW from the url:

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://example.com$1 [R=301]

But for some reason it doesn't work. Any suggestions?

Edit
Report

1 Answer

57

Here’s a more generalized solution:

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
answered 2010-03-02T07:58:53.170

Your Answer