301 Redirect with .htaccess

If you want to perform SEO chances are you’ll need to use a 301 redirect at some stage. If you’re using Apache web server you’ll probably have to do this through the .htaccess file. This blogpost explains why we use 301 redirects and how to execute them on an Apache server.

What is a 301 redirect?

Basically it is a way of telling a web browsers or search engine that a web page or site has been permanently moved to a new location.

What is a .htaccess file

According to Wikipedia:

In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration.

In English:

It’s a file placed at the root level of your website that allows you to specify things like Redirecting URLs, Rewriting URLs, Blocking users, Password protecting files etc. This post is going to focus on redirecting URLs specifically using a 301 redirect.

Why would I want to do this?

You need to do this for SEO purposes, to avoid broken links, 404 pages and duplicate content. Below are some of the circumstances where  301 redirects are often used.

How do I do this?

First things first. You’ll have to download your .htaccess file and open it up in text editor like Notepad.You should have mod_rewrite installed on your server and you should place the following lines in you .htaccess file.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

This turns rewrite on and sets the base URL for per-directory rewrites.

What kind of redirect you use depends on your situation. Using the examples above I’ll show you how to perform the most common 301 redirects.

You’ve modified a filename/URL

This is the most basic example of using a 301 redirect. If you’ve changed a filename or URL you will need to rediect the old one so users don’t get a 404 when coming from search engines or refering sites. You also want to pass on PageRank to the new URL. Lets say you changed a filename from bestwebsitedeals.html to bestwebdesignoffer.htm here’s how you’d do it.

redirect 301 /bestwebsitedeals.htm/ http://www.mydomain.com/bestwebsitedeals.htm/

In english: ‘Permanently redirect this old URL to this new URL’.

You’ve moved a file to a different folder/category

What if you don’t want to remane a file but want to place it in a subfolder or move it into a different folder? See below:

redirect 301 /bestwebsitedeals.htm/ http://www.mydomain.com/web-design/bestwebsitedeals.htm/

Note: If you have a dynamic website this is the same as changing category or assigning a new parent item depeding on you URL structure.

You’ve changed domain name

Lets say you’ve changed your domain name  from joesoap.com to mynewcompanyname.com. The domain joesoap.com is indexed in search engines, it has incoming links and has built up PageRank.

What you need to do is permanantly redirect all traffic from joesoap.com to your new domain. You also want to pass on the PageRank you’ve built up throughout the years to your new address. Place the following code in your.htaccess file to achieve this:

redirect 301 / http://www.mynewcompanyname.com/

What this says is ‘Permanently redirect everything  on this site to http://www.mynewcompanyname.com/.

You only want to use 1 version of your domain name

This is what is known as a canonical redirect and either redirects the non www. version of your site to the www. version or vica versa. This is vital to avoid duplicate content issues. If you don’t believe me ask Google.

Redirect www to non-www:


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

Redirect non-www to www:

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

You only want to use 1 version of you homepage

Quiet often a website will have a number of different URls for it’s homepage. For example:

http://mydomain.com/

http://mydomain.com/home.htm

To avoid duplicate content issues here use a 301 redirect.

redirect 301 /home.htm/ http://www.mydomain.com/

I hope this was of some help. Please contact us to find out more about our how we can identify how 301 redirects can help you. Alternatively visit our Search Engine Optimisation Services page.

Cheers!

Share and Enjoy:
  • Print this article!
  • E-mail this story to a friend!
  • RSS
  • Google Bookmarks
  • Facebook
  • Twitter
  • LinkedIn
  • Technorati
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Digg

Tags: , , ,

About Dave Dungan

Dave is an Internet Consultant with experience in web design, online marketing, search engine optimisation (SEO) and social media. He has been working in the Internet and Technology sector for 2 years and was previously employed with a prominent Irish social media company. Dave has a passion for internet technology and its application in the fields of marketing and communications.

No comments yet.

Leave a Reply