A PHP Redirection Script
This script is intended to be used as an apache 404 error page. When an html document is not found, it will try to find the same document with the php extension, otherwise it will redirect the user to your home page. Save this script as redirect.php and create a file called “.htaccess” in your root directory with the following line: ErrorDocument 404 /redirect.php. Any time a user requests a missing file it should redirect them appropriately.
<!-- delete this line
<?php
ob_start();
?>
<html>
<body>
<?php
$redirect_url='';
if (isset($_SERVER['DOCUMENT_URI']))
{
if (ereg('.html$',$_SERVER['DOCUMENT_URI']))
{
$redirect_url = 'http://'.$_SERVER['HTTP_HOST'];
$redirect_url = ereg_replace('.html$','.php',
$_SERVER['DOCUMENT_URI']);
if (isset($_SERVER['REDIRECT_QUERY_STRING']))
{
$redirect_url .= '?' .
$_SERVER['REDIRECT_QUERY_STRING'];
}
if (!headers_sent())
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$url);
header('Connection: close');
}
echo '<meta http-equiv="refresh" content="0;url='.
$redirect_url.'">'."rn";
echo 'HTTP/1.1 301 Moved Permanently<br>';
echo 'Redirecting to <a href="'.$redirect_url.'">'.
$redirect_url.'</a><br>';
}
else
{
print_not_found();
}
}
else
{
print_not_found();
}
function print_not_found()
{
$redirect_url = 'http://'.$_SERVER['HTTP_HOST'];
if (!headers_sent())
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.
$redirect_url);
header('Connection: close');
}
echo '<meta http-equiv="refresh" content="0;url='.
$redirect_url.'">'."rn";
echo 'HTTP/1.1 404 Not Found<br>';
echo 'Redirecting to <a href="'.
$redirect_url.'">'.
$redirect_url.'</a><br>';
}
?>
</body>
</html>
<?php
ob_end_flush();
?>
Categories: PHP