【发布时间】:2011-09-15 09:40:25
【问题描述】:
我必须使用自定义 404 页面进行 url 重定向(.htaccess 不是一个选项)。我正在考虑使用下面的代码。我试图通过在 URL 中添加一个标志来解释有效的重定向,以及找不到页面的真实情况。你怎么看?
<?php
// check GET for the flag - if set we've been here before so do not redirect
if (!isset($_GET['rd'])){
// send a 200 status message
header($_SERVER['SERVER_PROTOCOL'] . ' 200 Ok');
// redirect
$url = 'new-url/based-on/some-logic.php' . '?rd=1';
header('Location: ' . $url);
exit;
}
// no redirection - display the 'not found' page...
?>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404:</h1>
<h3>Sorry, the page you have requested has not been found.</h3>
</body>
</html>
编辑 - .htaccess 不是一个选项的原因是因为我在没有 apache 的情况下在 IIS6 上运行。
【问题讨论】:
-
@hakre: o.O 正在努力解析,伙计。
-
您确定要重定向到 404 错误处理程序中的现有文件以获取漂亮的 URL 吗?这不会模仿 URL 重写。 @tomalak:谢谢提示。
-
您可以使用ISAPI_Rewrite 3,它将通过 .htaccess 为您的 IIS6 服务器带来 mod_rewrite 支持。它有免费的 Light 版本。
标签: php url-rewriting url-routing http-status-code-404