include_once 这个文件在 404.php - 确保 404.php 配置为在有 404 时调用:
//ini_set('display_errors',2);
//ini_set('log_errors',0);
//ini_set('error_reporting',E_ALL);
$username = "dbuser";
$password = "dbpasswd";
$hostname = "db.hostname.com";
$database = "dbname";
$table = "tablename";
$curl = "http://". $_SERVER['HTTP_HOST'];
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL". mysql_error());
mysql_select_db($database) or die('Could not select database' . $database );
$query = 'SELECT durl FROM urls where surl="' . $curl . '";';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$durl = mysql_fetch_array($result);
if(isset ($durl) && $durl !=""){
header("HTTP/1.1 301 Moved Permanently");
header("Location:". $durl);
}
//else{
// echo "404 Not Found ";
//}
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($dbhandle);
?>
表结构:
CREATE TABLE tablename (surl varchar(256),
durl varchar(256));
surl = 源网址
durl = 目标网址