Force the use of ‘www’ in your URL.

This is a pretty simple use of mod_rewrite to force the use www in your urls.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]\

The first line is only needed if mod_rewrite isn’t enabled on your server (though the module does need to be enabled). Replace example.com with your actual domain name of course.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in quickfire and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted March 13, 2009 at 10:08 am | Permalink

    I have this one too.
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.example.com$
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

    I think you have to be careful with HTTPS though because if you access https://example.com/securepage you’re going to get http://www.example.com/securepage

  2. Posted July 22, 2009 at 2:16 pm | Permalink

    I used to use the one I posted above all the time. I haven’t tried it recently but it doesn’t work on Mosso/Rackspace Cloud. The one in the post does.

Post a Comment

Your email is never published nor shared. 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>