Spiga

Redirects with PHP

Today, I thought I would knock out a relatively light subject before tackling heavier stuff in the upcoming weeks and months. That topic is implementing header redirects with PHP.

Header redirects are redirects that are handled instantaneously and transparently by the browser, and come in two common forms: 301 redirects and 302 redirects.

The difference between the two is that a 301 redirect is used to tell users that a particular page has permanently moved, and a 302 redirect is used to tell users that a page has only been moved temporarily.


To implement a redirect, the following code must be placed in your PHP script before it prints out any output. Because the redirect is sent in the HTTP header, if the PHP script outputs anything before the redirect header is set, PHP will automatically send headers along with the data that is printed out, and it is impossible to add other headers after data is printed.
301 Redirect:

<?
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.suresh-mobileweb.blogspot.com/");
?>


302 Redirect:

<?
header("HTTP/1.1 302 Moved Temporarily");
header("Location: http://www.suresh-mobileweb.blogspot.com/");
?>


One important thing to keep in mind when considering putting up a redirect, is that search engines will generally follow and pass link popularity on through 301 redirects, but not 302 redirects, so if the redirect is more or less permanent and you would like the search engine indexing/ranking to reflect that, then a 301 is the way to go.

0 comments: