How to 301 Redirect your URL’s

February 28th, 2008 by Mike Shannon

One of the most important techniques that a web master can use to help make their site more search friendly is to properly implement 301 redirects. 301 redirects can be used to tell a search engine that301 redirect signs page “A” is really supposed to resolve to page “B”, or we can even make it so that domain “A” should resolve to domain “B”, if needed.

But why should we care about page or domain redirection in the SEO world?

  • Cuts down on duplicate content issues.
  • Helps to transfers link juice into a concentrated areas on your site
  • Helps get your pages indexed
  • Helps your site Rank Better - can brings in more traffic

Here are some apache .htaccess and PHP examples of page 301 redirecting that I’ve used time and time again:

301 Redirct page A to page B

Redirect 301 /old.html http://yourdomain.com/new.html

You will also note the “/” before the old.html, which represents your root web directory. The “/” is necessary or your web server will not know where to find the old file. The command also requires you to provide the full URL for the new page.

Redirect page(s) from site A to the same page(s) on site B

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$ [L,R=301]

This is a good way of transfering link juice from a domain name that you just bought to another existing domain you already own. The newly purchased site could have many links already built up over time and by properly 301 redirecting all page URLs to your existing site you are better able to take advantage of your purchase.

Note that the “.*” on the RewriteCond above which will catch any subdomain such as “www” and redirect that as well to the new domain.

Redirect all domains to a single domain

# Turn on apache mode re-write
Options +FollowSymLinks
RewriteEngine on

# 301 redirect any domain that is not www.yourdomain.com to www.yourdomain.com
# EX: if olddomain.com is requested then it will be redirected to www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

This is good if you have more than one domain name pointing to the same web root directory and want to make sure that all requests to all domains get routed (301 redirected) to a single domain. You may run into this situation if you have a new domain that you want to use in place of your old one but want to transfer all the link juice from the old domain into the new one. Note, this solution doesn’t 301 redirect old page URL’s, it just redirects at the domain level.

non-www to www

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

This 301 redirect can keep your site from tripping duplicate content filters by search engines. Imagine that you did not implement a 301 redirect from non-www to www, then suppose people were to link to a page on your site using non-www and www at the same time from different places… then search engines can get confused as to which URL to throw into their index and since they have 2 URL’s with the exact same content on your site they will see it as duplicate content - and that’s your fault, leading to the engines probably not trusting it = your page’s SERP rankings will drop.

www to non-www

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]

Redirect dynamic URL to static looking URL

RewriteRule ^product.php?id=(.*)&category=(.*)$ /$2/newpage.htm [L,R=301]

As an example, here we are redirecting http://www.yoursite.com/product.php?id=231&category=seo

to

http://www.yoursite.com/seo/newpage.htm

This kind of 301 redirection is pretty crucial for a lot of shopping cart type web applications that utilize variables in their URL strings. It’s better to leave out unnecessary information in the URL string that a search engine can’t use for indexing - such as an ID of your product or some other random variable specific to the internals of your site that would otherwise mean nothing to human searchers.

Redirect .htm pages to .php pages

RewriteEngine on
ReweiteBase /
RewriteRule (.*).htm$ /$.php

This code can also be used for different page extensions, just replace the htm with your old page extension and replace php with your new page extension.

Redirect all pages in a folder to a single URL

RewriteEngine on
RewriteRule ^oldproduct(.*)$ /new-landing-page.php [L,R=301]

This could be good for example, if you once had a product that you no longer carry but you still want to transfer any link juice from those old pages into another single page somewhere in the local directory.

Redirect page A to page B using PHP

<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.new-url.com” );
?>

PHP is nice in that it is a server side language (executed before the HTML is returned the search engine) and you don’t need to mess with your .htaccess file or even have an apache server to 301 redirect using this method.

So that’s it for now. I’m sure there are many other methods or cases where you may want to 301 redirect pages… if so let me know and I’ll add them to this post. Happy 301-ing.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netscape

3 Responses to “How to 301 Redirect your URL’s”

  1. Affordable SEO Services Says:

    Great post and very important stuff for SEO purposes. FYI, if you are using asp or asp.NET you can download some very inexpensive software ( http://www.isapirewrite.com/ ) and use a http.ini rather than a .htaccess file to handle all rewrites and redirections.

    Dragons Byte SEO Services at http://www.dragonsbyte.com

  2. jazzie Says:

    I heard so much about bestrank.com. My friends are telling me about it. I’m very much interested to learn more about this site. These stuffs are new to me so I’m a bit curious about it and the benefits that I can get from reading here. More power bestrank!!!

  3. SNVC Says:

    This is definitely a good guide. Thanks for this.

Leave a Reply


Entries (RSS) and Comments (RSS).