Home > Tips > Website Design & Content Tips > 301 Redirects
301 Redirects - Get in Good with Google
Updated August 22, 2007

Over at Aaron Wall's site, SEO Book, there was a killer article from Tony Spencer on how to do 301 redirects. This is always a great topic as Google still can't quite figure out if they are going to fix the canonicalization problem or not.
So, here are Tony's comments, with my commentary included.
301 non-www to www
What is the canonicalization problem? Simply put, it is when the www version of your site gets indexed along with the non-www version (i.e. http://www.domain.com & http://domain.com).
To fix it, it is really simple. Just go into your .htaccess file and add the following:
<code>
RewriteEngine On
RewriteCond %{HTTP_HOST}
^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</code>
Remember to sub "your domain" for "domain" in the above example, or it just isn't going to work.
The '(*.)$' says that we'll take anything that comes after http://seobook.com and append it to the end of 'http://www.seobook.com' (thats the '$1' part) and redirect to that URL.
Note: You only have to enter 'RewriteEngine On' once at the top of your .htaccess file, not for every entry.
Alternately you may chose to do this 301 redirect from
inside the Apache config file: httpd.conf. It is recommended to do this if your .htaccess file is too large, as it tends to make your server run a bit slower.
Here is the code to add to your htpd.config file:
<code>
<VirtualHost 67.xx.xx.xx>
ServerName www.domain.com
ServerAdmin webmaster@domain.com
DocumentRoot /home/domain/public_html
</VirtualHost>
<VirtualHost 67.xx.xx.xx>
ServerName domain.com
RedirectMatch permanent ^/(.*) http://www.domain.com/$1
</VirtualHost>
</code>
Note that often webhost managers such as CPanel would have placed a 'ServerAlias' domain.com in the first VirtualHost entry which would negate the following VirtualHost so be sure to remove the non-www ServerAlias.
If that is Greek to you, you might be best served to get a good host manager like Easy Server Management that we use. Seriously, if it wasn't for those guys, I don't think we'd be as successful as we are. It isn't that they are that good, which they are, they just take away all the stress, and that feeds our creativity.
301 www to non-www
Finally the www 301 redirect to non-www version would look like:
<code>
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
</code>
By doing this, you will see a lot of great things happen with Google. First of all, you will see that the speed that they index your site will increase, and the duplicate content pages will be reduced (takes 4-6 weeks).
Not to mention your site just looks better in the URL bar with the "www" in there.
Redirect All Files in a Folder to One File
Lets say you no longer carry 'Super Hot Product' and hence want to redirect all requests to the folder /superhotproduct to a single page called /new-hot-stuff.html. This redirect can be accomplished easily by adding the following your .htaccess page:
<code>
RewriteRule ^superhotproduct(.*)$ /new-hot-stuff.html [L,R=301]
</code>
But what if you want to do the same as the above example EXCEPT for one file? In the next example all files from /superhotproduct/ folder will redirect to the /new-hot-stuff.html file EXCEPT /superhotproduct/tony.html which will redirect to /imakemoney.html
<code>
RewriteRule ^superhotproduct/tony.html /imakemoney.html [L,R=301]
RewriteRule ^superhotproduct(.*)$ /new-hot-stuff.html [L,R=301]
</code>
This is a very handy tip and the code is nice and lean and it makes for a quick copy, paste and edit job. You can be in and out in less than five minutes.
Redirect a Dynamic URL to a New Single File
It's common that one will need to redirect dynamic URL's with parameters to single static file:
<code>
RewriteRule ^article.jsp?id=(.*)$ /latestnews.htm [L,R=301]
</code>
In the above example, a request to a dynamic URL such as http://www.domain.com/article.jsp?id=8932
will be redirected to http://www.domain.com/latestnews.htm
This is true to be "search engine friendly" but don't do this for each dynamic URL on your system, as you could have hundreds or thousands of entries. Instead, you want to bake up a nice script that will do it on the fly for you. That is a little more advanced than we are going to get into here.
SSL https to http
This one is more difficult but I have experienced serious canonicalization problems when the secure https version of my site was fully indexed along side my http version. I have yet to find a way to redirect https for the bots only so the only solution I have for now is to attempt to tell the bots not to index the https version. There are only two ways I know to do this and neither are pretty.
1. Create the following PHP file and include it at the top of each page:
if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') { echo '<meta name="robots" content="noindex,nofollow">'. "\n"; }
2. Cloak your robots.txt file.
If a visitor comes from https and happens to be one of the known bots such as googlebot, you will display:
User-agent: *
Disallow: /
Otherwise display your normal robots.txt. To do this you'll need to alter your .htaccess
file to treat .txt files as PHP or some other dynamic language and then proceed to write
the cloaking code.
I really wish the search engines would get together and add a new attribute to robots.txt
that would allow us to stop them from indexing https URLs.
After all, it is OUR content, isn't it?
Getting Spammy With it!!!
Ok, maybe you aren't getting spammy with it but you just need to redirect a ton of pages. First of all it'll take you a long time to type them into .htaccess, secondly too many entries in .htaccess tend to slow Apache down, and third its too prone to human error. So hire a programmer and do some dynamic redirecting from code.
Not only is it effective, but it is killer when it is done, and you can show it off to your friends. Oh yeah, they won't get it.
The following example is in PHP but is easy to do with any language. Lets say you switched to a new system and all files that ended in the old id need to be redirected. First create a database table that will hold the old id and the new URL to redirect to:
old_id INT
new_url VARCHAR (255)
Next, write code to populate it with your old id's and your new URLs.
Next, add the following line to .htaccess:
<code>
RewriteRule ^/product-(.*)_([0-9]+).html /redirectold.html?productid=$2
</code>
Then create the PHP file redirectold.html which will handle the 301:
<code>
<?php
function getRedirectUrl($productid) {
// Connect to the database
$dServer = "localhost";
$dDb = "mydbname";
$dUser = "mydb_user";
$dPass = "password";
$s = @mysql_connect($dServer, $dUser, $dPass)
or die("Couldn't connect to database server");
@mysql_select_db($dDb, $s)
or die("Couldn't connect to database");
$query = "SELECT new_url FROM redirects WHERE old_id = ". $productid;
mysql_query($query);
$result = mysql_query($query);
$hasRecords = mysql_num_rows($result) == 0 ? false : true;
if (!$hasRecords) {
$ret = 'http://www.yoursite.com/';
} else {
while($row = mysql_fetch_array($result))
{
$ret = 'http://www.yoursite.com/'. $row["new_url"];
}
}
mysql_close($s);
return $ret;
}
$productid = $_GET["productid"];
$url = getRedirectUrl($productid);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
exit();
?>
</code>
Now, all requests to your old URLs will call redirectold.html which will lookup the new URL and return a HTTP response 301 redirect to your new URL.
Using 301 Redirects is something that you need to learn. Even if you have a programmer, you need to learn it so you can understand the concepts, so when something happens, your brain can get into gear and formulate a strategy you can pass onto your programmer, and POOF, your problem is gone.
We will be doing a video that will show you how to actually put some of the examples above into action and show you how easy it is to modify your .htaccess file.
But you can only get it by being a member of the SEO Revolution. The Revolution is Calling You!
Click the banner below to get access.

|