How to do 301 redirection

I have joined Google and none of the tools on eKstreme.com are official Google tools nor are they endorsed by Google. See this.

There are many ways to do 301 redirection. Some are better for forwarding whole websites and domains, and others are better used for forwarding single web pages. We will now discuss three different methods for 301 redirection:

301 redirection with PHP

Using the PHP header function, it is possible to do 301 redirection.

To forward www.mywebsite.com to mywebsite.com, use the following code:

<?php
 if(stristr($_SERVER["HTTP_HOST"], 'www')){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://mywebsite.com/" . $_SERVER["REQUEST_URI"]);
  exit();
  }
?>

To forward mywebsite.com to www.mywebsite.com, use the following code:

<?php
 if(!stristr($_SERVER["HTTP_HOST"], 'www')){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.mywebsite.com/" . $_SERVER["REQUEST_URI"]);
  exit();
  }
?>

Remember to replace "mywebsite.com" with your correct website domain name :)

Some things worthy of notice:

This code is best used for single pages that require forwarding, and not whole websites. To use it, just add the code at the very beginning of the page's PHP file. Make sure there are no spaces or any other characters before the opening <?php, otherwise, the code won't work and you'll get a warning.

Apache mod_rewrite and 301 redirection

One of the most powerful Apache modules is the mod_rewrite, which allows you to rewrite URLs. By rewriting a URL, you can completely control its new destination. Another bonus of using mod_rewrite is that since it works at the server level, it can be used to forward whole websites.

To use mod_rewrite, your server needs to support it. To find out, either use the PHP phpinfo() function or ask your hosting provider. Assuming you have mod_rewrite available, add the following code to your .htaccess file (more on that in a bit).

To forward mysite.com to www.mysite.com, use the following code:

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

To forward www.mysite.com to mysite.com, use the following code:

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

The .htaccess file

The Apache webserver uses .htaccess files to tweak its configuration while the server is running. The .htaccess file is a very versatile tool, but misusing it can make your site malfunction, so be careful!

The placement of the .htaccess file limits it effects: if you put it in your document root, it affects all of your website, but if you put it in a subdirectory, it affects that subdirectory and everything in it.

The .htaccess file is a simple text file, so edit it using a simple good text editor.

Wrapping up 301 redirection

I hope that by now you understand a lot more about redirection on the internet. It is a hidden aspect, but one that is very important for search engine optimization and good website ranking.

We learned why we need correct redirection and how we can implement that. We learned about PHP, about mod_rewrite, and we learned that some nice hosting companies allow us to keep our hands clean and do it automatically for us.

Of course, if you have any suggestions to make this tutorial better, please drop me an email. If you are still lost, I strongly recommend that you visit the Cre8asite forums for help.