PHP 301 Redirection – Web forwarding done right

Print Friendly, PDF & Email

HTTP RedirectionPHP 301 Redirection

Redirection is usually better done via .htaccess files, but in case your hosting provider doesn’t support this method, then you can achieve simple redirection on a per-page basis using PHP. This is the method I am going to discuss in this article.

When to use redirection

Lets’s say you had a page that was quite popular, but it had to move to a different place in your website. It is listed in google, and you don’t want to loose out by the page being unavailable after you delete the old one. Rather than just keeping the old page, and putting a message on it saying “this page has moved” (yuk!) – it would be much nicer if we could tell google (the the rest of the world) that your page has permanently moved.

Types of redirection

There are many redirection, and many articles describing in painful details how they work, and how you should use them. Suffice to say, that for most people the only ones you ever need to think about are:

  • 301 – Permanent – hard redirection, telling the browser that the current URI is now outdated and th new URI should be used for future requests.
  • 302 – Found – Bit of an odd one, and not strictly a temporary redirection, but due to the way many search engines work (still using HTTP/1.0 requests), this is still a workable status code for redirecting temporarily (for example, if an area of your site is temporarily rehomed). 302 redirects have a chequered history having been badly abused by internet marketeers, so most search engines do not handle 302 redirects in a standards compliant manner – and in fact may ignore them depending on the nature of your content, and the content of the target page.
  • 307 – Temporary – Google, and most other more prominent search engines now understand 307 temporary redirects as their spider bots now use HTTP/1.1 requests.

It is one of the worst implemented areas of most websites, and many people thing that 301 is always the way to go, but this just isn’t true. 302 and 307 are useful redirects in many circumstances – but it depends on the goals you are trying to achieve. Also, many people just don’t bother putting a proper status code in their redirects, using something like:

Header( "Location: http://www.domain.com/new-page.php" );

without sending the status code beforehand will just result in a default soft 302 redirect – which is often not at all what you really want to do.

How to do it in PHP

The right way to do it is firstly to ensure that you DO NOT send any data before the HTTP redirect, and terminate the script after the last header statement. If you have warning and errors turned on in apache then you will see why this is a bad idea – but it really doesn’t make life easy for search engines if they get sent a pile of crap with your redirect embedded in there somewhere… so just keep it simple:

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.newsite.com/new-page.php" );
exit();
?>

If you see an error message like this:

Warning: Cannot modify header information - headers already sent by ...

then you have some whitespace before your opening <?php tag – go back and remove it! The above script will tell the world that the page being accessed has permanently moved somewhere else. That’s really all there is to it.

PHP redirection isn’t always idea, especially if you need to redirect a lot of file – a better method is to use .htaccess and mod-rewrite – but that’s another article for another day!

Tags: ,

One Response to “PHP 301 Redirection – Web forwarding done right”

  1. Josh May 31, 2010 at 5:20 pm #

    Nice simple explanation of the different redirects. Didn’t know about 307.

Leave a Reply

Bot test * Time limit is exhausted. Please reload the CAPTCHA.