Remove “www” from site URL with mod_rewrite
I needed to have my gallery URL “www” free, due to some dns problems. So, I searched the Internet and got the solution. If you want you can try this link http://www.nicusor.com/gallery to see it in action. You can find the code for removing and adding the “www” below.
Here is the code to redirect (301) the www version of your site to the non-www version using Apache’s mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.your-site.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://your-site.com/$1 [R=301,L]
</IfModule>
That L flag means Last rule = stop the rewriting process here and don’t apply any more rewriting rules.
Here is the code to redirect (301) the non-www version of your site to the www version using Apache’s mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-site.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.your-site.com$1 [R=301,L]
</IfModule>
Hope it helps!
Tags:mod rewrite , programming stuff , seo , tipPopularity: 32% [?]




Nice little tip. I never thought it made any difference as to whether the www. was actually included or not, is that just the way of telling the browser what protocol you are using with www. being the default?