I recently converted several sections of my site from html to php, and I wanted an easy way to change over without uploading a redirect page in the place of each html page. So I came up with this option.
Required:
Your server must allow you to use php pages for error messages.
If that's not the default, you'll need access to the htaccess file to make the change.
You will need to delete the .html files from the folders you are uploading .php files in their place.
For example, I have a folder at http://love-productions.com/about/. It previously had index.html, lp.html, k.html. Now it has index.php, lp.php, and k.php and I've deleted the html pages. So if you visit /about/index.html, it will redirect you to /about/index.php.
Here's what it does: A 404 page is what automatically comes up when the file isn't found. Using the server information for the link that was requested, I check to see if that URL has an .html extension. If so, then I check if a filename with the same name exists in that folder with a .php extension. If so, a meta tag redirects it to the new page. Otherwise, a normal "page not found" error is given. You'll want to customize the red letters below to have a link to your index page or search page.
<html>
<head>
<title>Page Not Found</title>
<?
//get the page searched for.
$root=$_SERVER["DOCUMENT_ROOT"];
$page = parse_url($_SERVER["REQUEST_URI"]);
$page=$page[path];
//find the extension of current page
$ext_array =explode(".",$page);
$last = count($ext_array) – 1;
$ext = $ext_array[$last];
if ($ext=="html") {
$ext = ".$ext";
$page=str_replace($ext,"",$page);
$extpage=$page.".php";
if (file_exists($root.$extpage)) {
echo "<meta http-equiv=\"Refresh\" content=\"1;url=", $extpage, "\">";
echo "<META name=\"robots\" content=\"NOINDEX, FOLLOW\">";
}
}
?>
</head>
<body>
<h1>Page Not Found</h1>
If possible, the browser will try redirecting you to the appropriate place. Otherwise, the file you clicked isn't found!
Try going to the <a href="http://yoursite.com">index page</a>
</body>
</html>
If you need to modify your server's htaccess file, you'll want to add this line to it so it recognizes the .php file instead of the normal 404 page:
ErrorDocument 404 /404.php
I haven't tested this on any other sites that mine, so it probably should be considered a beta script until several people test it out