Force www. in your URL using .htaccess

Insert this into the top of your .htaccess file, making sure that ‘RewriteEngine On’ comes before it:

RewriteCond %{HTTP_HOST} !^www\.jhuskisson\.com [NC]
RewriteRule ^(.*)$ [R=301,L]

Replace jhuskisson.com with your chosen domain and make sure if you are placing any full stops within the first line to enter a backslash before each full stop. This will prevent the code from not working.

Like this site? Subscribe on Facebook and follow on Twitter

Share this with friends:

Like what you've just read? Share it to your friends using your favourite service below:

Share on Stumble Upon Share on Google Share on MySpace Share on Delicious Share on Digg Share on Google Buzz Share via Reddit Share via E-Mail Share via Wordpress.com Share via Tumblr Share via Posterous Share via Newsvine

Comments:

  1. I tend to prefer the reverse (something about the URL length, I think.) Either way, great tip!

    Is there a reason you prefer having the “www” over the alternative?

  2. Think I’ve seen it for so long it just feels un-natural without it Ryan ;)

  3. So instead of adding the www. your code removes it Maciej?

    Thanks for contributing that – I’m sure people here will be looking for both alternatives :)

  4. I’ve tried both, but the solution has caused other problems. I’ve installed bbpress, but the users that log into Skinplistic WITHOUT the www are not recognized by users that log into bbpress WITH the www. I used the code above to force access via www, however; I noticed this counted multiple hit against my server (I use slimstat). Plus, I think it may be upsetting adsense and the other robots I prey for everyday. ;) I don’t know what to do.

  5. this solution provided is only for http, make sure you cover https as well..heres the code I’ve been using for quite some time:

    RewriteCond %{HTTP_HOST} ^overfrown\.com
    RewriteCond %{HTTPS} =on
    RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
    RewriteCond %{HTTP_HOST} ^overfrown\.com
    RewriteCond %{HTTPS} !=on
    RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]

  6. #kyle renfrow

    thank you, the other redirect chops the path before the script name off (in my case at least)

  7. Thanks Jamie and Maciej for the simple documentation. I’ve referred to it several times myself.

  8. Thanks for this, I’ll get it up on my site now :)

  9. HI this is my current .htaccess file. The solution above doesnt seem to work for me. the site doesn’t load up when I have made the changes. what am I missing? thanks

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

  10. Many thanks for this – I’ve used it successfully on my site.

  11. @kyle renfrow
    Thanks for your suggestion!!! IT ROCKS DUDE!!!

Pingbacks/Trackbacks:

  1. 16th of June 2010

    [...] a plugin; Force www. (without .htaccess) – vBulletin.org Forum or here's how using htaccess; Force www. in your URL using .htaccess : Blog : Jamie Huskisson __________________ Mikey / vBulletinSetup Staff Validate your vBSetup Account. Click Here. [...]

Add a Comment: