如何将浏览器重定向到数据库中的页面
问题描述:
我知道如何使用HTML中的“刷新”元标记进行重定向。我的虚拟主机(Bluehost)也会查找404.SHTML页面,以防文件未找到。但是,它似乎忽略了404.PHP。如何将浏览器重定向到数据库中的页面
我想截取我域中的无效URL,并重定向到我在数据库中查找的相应页面。有点像bit.ly和其他缩写词。
由于我需要访问数据库,我想使用PHP,但就像我说的我的虚拟主机只支持404条消息的HTML。
示例:用户键入“example.net/123”,其中目录123不存在。所以我想在我的数据库中查找“123”,并且它给出了“foo.php”作为对应的页面,所以用户被重定向到“example.net/foo.php”。 我该怎么做?
答
好的,这是我所做的。首先,我重定向强制404.shtml我自己404.php页面:
<html>
<head>
<title>404 Not Found</title>
<script type="text/javascript">
window.location.href = "http://mydomain.com/404.php";
</script>
</head>
<body>
</body></html>
显然没有必要通过引用者,因为即使没有这样做404.php会看到原来的引荐,不404.shtml。很方便。所以我所要做的就是在数据库中查找新的url。这是我的404.php:
<?php
$url = $_SERVER['HTTP_REFERER'];
//extract the reference from the url
$abbrev= substr($url, 20, 1000);
$username="********";
$password="********";
$database="********";
mysql_connect("localhost", $username, $password);
@mysql_select_db($database) or die("unable to select database");
$query="SELECT * FROM fwd WHERE abbrev =" . $abbrev;
$result=mysql_query($query);
$url = mysql_result($result, $i, "url");
header('Location: ' . $url);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>redirect</title>
</head>
<body>
</body>
</html>
当然,我要检查的缩写是否在数据库中,如果它是不是显示404消息(这就是为什么HTML代码仍然存在),但这是基本设置