How to do 301 redirection

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:

  • Using the PHP header() function if your webserver supports PHP.
  • Using the mod_rewrite functionality of Apache servers.
  • Using built-in forwarding if your host provides it.

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:

  • Notice how we send the HTTP 301 header and then we immediately follow it with the new location.
  • Also, notice that we forward the REQUEST_URI value. The REQUEST_URI is the path within the website to the web page. For example, in http://ekstreme.com/phpcounter/index.php, the /phpcounter/index.php part is the REQUEST_URI. Our forwarding code above forwards to the user to the page they requested.
  • Notice the exit() statement: this makes sure that the PHP code following this code does not get executed and that only the forwarding information is sent to the browser.

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. For recommendations, see the list of freeware I keep.

More tips about .htaccess in this SEOChat forum thread and this thread.

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.

Comments are closed.

 

Site Navigation

Popular Pages

The most popular pages on eKstreme.com.

Search

Subscribe

Subscribe to RSS 2.0 feed