0

我使用 .htaccess RewriteRules 将 url 传递给 index.php。如果在数据库中找不到指定的页面名称,我希望我的脚本抛出正确的 404 响应。

我试图实现这一目标的方式是:

header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");

在我的浏览器中分析 HTTP 响应,响应返回正常:

HTTP/1.1 404 Not Found 

但是,我希望能够输出一个友好的 404 页面,但是 header() 函数之后的任何内容都被忽略,以及之前的任何内容......好吧......那太愚蠢了。

谁能建议如何从脚本中输出 404 错误消息?

谢谢。

4

3 回答 3

3

You have to make sure the output (or ErrorDocument) is larger than 512 bytes as to make sure Internet Explorer doesn't display it's own error-page. This is probably why you have experienced any output after the header doesn't get displayed.

于 2010-09-11T13:26:53.257 回答
1
<?php
header("HTTP/1.0 404 Not Found");
    echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL " . $_SERVER['REQUEST_URI'] . " was not found on this server.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at " . $_SERVER['SERVER_NAME'] . " Port 80</address>
</body></html>";
    exit();
?>
于 2010-09-11T13:24:21.070 回答
0

所以跟随(在.htaccess中)

ErrorDocument 404 /errordoc.html

不适合你吗?

编辑:如您所见,您可以轻松地在 PHP 中抛出 404,然后在 .htaccess 中捕获它。

于 2010-09-11T13:20:02.197 回答