submitted by:

PHP: Permanent 301 Redirect - Basic

If the URL of a page is changed, a 301 permanent redirect should be created to point visitors to the new location and most importantly: help preserve existing search engine ranking. 301's are usually setup via the .htaccess file, but you can also use a .php script


code snippet:
<?php

//important: this defines the redirect as a 301
header"HTTP/1.1 301 Moved Permanently" );
header"Status: 301 Moved Permanently" );

//redirect to the new location
header"Location: http://www.example.com/mynewpage.html" );
exit();

?> 

notes:
In this example, the php script is saved in the location of the old url, for example www.example.com/oldpage.php. Any visitors to this URL will be redirected to www.example.com/mynewpage.html This is a simple example, but with some tweaking the same approach will work with dynamic urls.