Let's say you have an old web-page that's no longer in use. Of course, you'll want to redirect your users to the correct (new) page. Using PHP, you can modify the headers of a web-page to redirect them to the correct page.
This will cause the user who visits the web page with this code, to be redirected to http://www.mydomain.com/newpage.html (or any other page you specify).
That’s all fine, and works well. But what if Google indexed your old web page, and still directs traffic to it? What if search-results in Google only display the old page, instead of the new one? What if, one day, you decide to take the old page offline?
To better understand the next part, let’s first explain what status codes are and what they mean to us, and Google. When a web page is successfully loaded, it sends your browser (IE, Firefox, …) a status code of “200", which means: the page exists, and was sent successfully. There’s also a status code called “404", which means the page you requested doesn’t exist. It’s these status codes that Google uses to keep track of pages that are moved, deleted or temporarily unavailable.
By default, a header(“Location: …")-redirect will pass a 302 status code, which means the page was moved temporarily, but will be back shortly. If Google sees such as page, it’ll keep it in its index, as it assumes it will once exist again. In this case, it’s better to pass a 301 status code, to explain the page moved permanently to another location.
To pass a 301 status code to the browser, you can do the following.
Once Google crawls that page, it’ll see it was moved permanently, and remove the current page from its cache (though this can take a while), and start displaying the new link in the search results – instead of the old one.
Another method to pass status codes is to use the additional parameters of the header() function. Doing the following, is essentially the same as the previous example, redirect to another page with a 301 – Moved Permanently status code.
You can do the same if you display a custom 404 page (by using .htaccess and the ErrorDocument directive, for example). By passing the necessary headers, you can tell the browser (and thus all search-indexing-bots out there) to no longer link to this page, as it doesn’t exist.
And you can still display your custom error page, to notify your visitors as well.
Using headers in PHP is definitely something worth spending a few minutes on, as it can help you create clean redirects/error pages, and keep your indexed pages limited to those that actually exist, and contribute to your website.
There’s a very useful Firefox Plugin called LiveHTTPHeaders that can help you test these headers, and see if they are used properly. It’ll show you the statuscode of the website you’re visiting, and can easily display 301, 302, 404, … statuscodes.