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]

博客从域名godpress.cn转到roygu.com域名

博客转来好几天了,但是一直懒于做域名301重定向。为了保留以前的读者,一般情况下,网站换域名后要做相关的SEO处理,最通常的就是把原来网站相应的URL全部转到新网站相应的URL上。 示例:你可以访问http://godpress.cn/?p=328,它会自动重定向到http://roygu.com/?p=328。不多说了,下面直接贴代码:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^godpress.cn$ [OR]
rewritecond %{http_host} ^www.godpress.cn [nc]
rewriterule ^(.*)$ http://roygu.com/$1 [L,R=301]