301 WWW Redirect

I think it’s a pretty safe thing to assume that most people are lazy when it comes to surfing the web. I know I am. One of the big ones I do is skip the www when typing in web addresses. It’s a waist of my precious and valuable time! I shouldn’t have to spend that extra 1.5 seconds when all I want is my daily fix of i can has cheezburger cuteness! Really!

‘Not a big deal’ you say… well you’re wrong! Probably the biggest issue is search engines like Google consider yoursite.com and www.yoursite.com different websites. Therefore, if your website has been linked to from other websites using a mix of the two URL’s you’re effectively splitting the potential benefit of valuable link popularity in half.

Using a 301 redirect on the www absent version of the URL can fix this problem right up.

To solve this, here’s a script that automatically redirects visitors on your website to www.yoursite.com.

You need to be able to access your .htaccess file on your website. It’s located in the root of your website (usually public_html or www)

As with changing any vital file on your website, make sure to make a backup of your .htaccess file before you start. You’ll be adding the following code:

Full Code

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule ^/?(.*)$ "http\:\/\/www\.yoursite\.com\/$1" [R=301,L]

Obviously replace yoursite.com with your actual site. And if RewriteEngine On is already in your .htaccess file, you don’t need to add it again. Just make sure you place the redirect code after it.

And voila! When people now visit your website without typing in www, they will be redirected automatically.

Also note, this only works on non Microsoft servers. Why you’d be on a Microsoft sever… *sigh*

June 27th, 2009 | Web Development

Leave a Reply

Name:
Email:
Website:
Message:
SUBMIT