March 30, 2008, 06:47:15 PM
Several months ago I changed from
Etomite to
Wordpress on my
ProofBuddy site. The biggest problem I came across was that about 50 pages needed to change their URI. I wouldn't have been difficult to edit the
.htaccess file and do the redirects but with 50+ of 'em I wanted to find a cleaner way.
What I came up with was using a 404 page. Wordpress has a 404 page built into the themes, but if you're not using Wordpress you can setup a
custom 404 to do the same thing. If you're going the custom 404 page route it does need to be a PHP file. You should be able to do the same in ASP or .Net but I don't know enough of either of those languages to know how.
First you need to create a list of the old URIs and the associated new URIs. Then you just plug in the URIs into an associative array with the old URI as key and the new URI as the value.
<?php
// Check for old page names
$arr_pages['old-page-1.html'] = '/new-page-1.html';
$arr_pages['old-page-2.html'] = '/new-page-2.html';
$arr_pages['old-page-3.html'] = '/new-page-3.html';
$arr_pages['old-page-4.html'] = '/new-page-4.html';
$arr_pages['old-page-5.html']= '/new-page-5.html';
// ... and so on ... //
$request = strtolower($_SERVER['REQUEST_URI']);
$tmp = explode('/', $request);
$request = $tmp[count($tmp)-1];
if (array_key_exists($request, $arr_pages))
{
header('HTTP/1.0 301 Moved Permanently');
header('Location: http://www.example.com'.$arr_pages[$request]);
}
?>
Your 404 page info goes here. It's only shown if the
page isn't in the array.
What happens is that the script pulls out the request URI from the address entered. If it matches any of the keys in the array $arr_pages then it does a 301 permanent redirect to the new page. If the page isn't found in the array then the 404 page is displayed.
Warning: Geek Content BelowFor those that have used header() along with Location: before you may notice that I'm sending a HTTP/1.0 301 Moved Permanently header first. That's because using Location: as part of the header sends a 302 Moved Temporarily header instead which is not what I wanted. By sending the 301 first the 302 doesn't get sent and everybody gets the right header.

Logged