如何将浏览器从数据库重定向到页面


How to redirect browser to a page from a database

我知道如何在HTML中使用"refresh"元标签重定向。我的网站主机(Bluehost)也寻找一个404。在没有找到文件的情况下,使用SHTML页面。但是,它似乎忽略了404.PHP。

我想在我的域中拦截无效的url,并重定向到我在数据库中查找的相应页面。有点像。Ly和其他缩写可以。

因为我需要访问数据库,我想使用PHP,但就像我说我的网站主机只支持HTML的404消息。

示例:用户输入"example.net/123",其中目录"123"不存在。所以我想在我的数据库中查找"123",它给出"foo.php"作为对应的页面,所以用户被重定向到"example.net/foo.php"。我该怎么做呢?

好的,我是这样做的。首先,我重定向了强制性的404。

<html>
<head>
<title>404 Not Found</title>
<script type="text/javascript">
    window.location.href = "http://mydomain.com/404.php";
</script>
</head>
<body>
</body></html>

显然不需要传递referer,因为即使不这样做404.php也会看到原始referer,而不是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代码仍然存在),但这是基本的设置