PHP header redirect

PHP header redirect Redirect users to another page or site using header: Location

At times, you may find yourself moving pages around or even changing domain names. Using the header function of PHP we can direct visitors (and search engines for that matter) to the new location with 3 lines of code.

<?php
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: http://www.example.com/newpage.php');
exit;
?>

You can redirect visitors to a page or another domain/sub-domain. It is very important that you include the exit portion at the end of the code. This prevents PHP from processing any further.

There are other redirect codes that you can use. The 301 code is the preferred method to avoid any issues with your current search engine rankings.

Status Code Definitions
301 Moved Permanently
302 Found (resource has temporarily moved)
303 See Other (resource found under a different URI and should be retrieved using GET)
305 Not Modified
307 Use Proxy

References:
PHP header function - http://us.php.net/header

Your rating: None Average: 2 (1 vote)