PHP 404 响应头
我使用 .htaccess RewriteRules 将 url 传递给 index.php。如果在数据库中找不到指定的页面名称,我希望我的脚本抛出正确的 404 响应。
我尝试实现此目的的方法是:
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
分析浏览器中的 HTTP 响应,响应返回正常:
HTTP/1.1 404 Not Found
但是,我希望能够输出友好的 404 页面,但 header() 函数之后的任何内容都会被忽略,以及之前的任何事情……好吧……那是愚蠢的。
谁能建议如何从脚本中输出 404 错误消息?
谢谢。
I use .htaccess RewriteRules to pass the url to index.php. If the specified page name isn't found in the database, I want my script to throw proper 404 responses.
The way I have tried to achieve this is:
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
Analysing the HTTP responses in my browser, the response comes back fine:
HTTP/1.1 404 Not Found
However, I would like to be able to output a friendly 404 page, but anything after the header() function gets disregarded, and anything before ... well ... that'd be stupid.
Can anyone suggest how to output a 404 error message from the script?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须确保输出(或 ErrorDocument)大于 512 字节,以确保 Internet Explorer 不会显示其自己的错误页面。这可能就是为什么您在标题未显示后遇到任何输出的原因。
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.
那么以下(在 .htaccess 中)
对您不起作用?
编辑:如您所见,您可以轻松地在 PHP 中抛出 404,然后在 .htaccess 中捕获它。
So following (in .htaccess)
is not working for you ?
EDIT: As you see, you can easy throw 404 in PHP and then catch it in .htaccess.