Redirect pages with PHP: redirect old URLs to new URLs
Save the following PHP code as 404.php and set this PHP page as your custom default 404 Not Found page. This will prevent visitors from getting ugly and unwanted default 404 Page Not Found messages.
<?php
// watch out for accepting request_uri input without validation/sanitation!
$request = $_SERVER['REQUEST_URI'];
$arrMoved = array(
"/php/guestbook/" => "/php/phpGuestbook/",
"/php/forumzz.php" => "/php/forum.php",
"/old.php" => "/new.php",
"/..." => "/..."
);
if(array_key_exists($request,$arrMoved)) {
$newplace = "http://".$_SERVER['HTTP_HOST'].$arrMoved[$request];
header("HTTP/1.0 301 Moved Permanently");
header("Location: $newplace");
header("Connection: close");
exit(); // ALWAYS use exit() after header()
}
else {
header("HTTP/1.0 404 Not Found");
}
?>
<!--
Your normal HTML code goes here, if a match is found the
visitor will have been redirected. Only genuine 404 errors will see the HTML below.
Follow me on Twitter: @Jan_Reilink
-->
<html>
<<head>
<title>
404 Error page
</title>
</head>
<body>
<p>Sorry but this page cannot be found.</p>
</body>
</html>