Easy way to redirect www.domain.com to domain.com

Useful quick tip for anyone needing to redirect pages. Usually its something I throw into sites to redirect them from www.mywebsite.com to mywebsite.com. I have run into many issues in the past where the cookie handling in software is limited to either domain.com or www.domain.com which is very annoying to the end-user as www.domain.com and domain.com are seen as two different sites and can knock you out of being ‘logged in’.

To easily remedy this, you can throw this into your .htaccess file:

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

Just replace domain.com with your domain name. This will do a google friendly 301 permanent redirect.




Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>