How to remove www from your URL with mod_rewrite

source link: http://yoast.com/how-to-remove-www-from-your-url-with-mod\_rewrite/ I got a hit today for the following search query: how do you get rid of the www in url .

Here's the code to 301 redirect the www version of your site to the non-www version using Apache's mod\_rewrite:

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

Adding the www instead of removing it

And, as requested in the comments, the code to add www to your domain name:

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