HTTP-404 标头似乎在服务器日志中不起作用
我已经构建了一个动态网站并修改了静态 URL 的 .htaccess 文件,并且我需要创建一个用于 SEO 的 404 页面。
header("HTTP/1.0 404 Not Found");
当数据库中没有匹配的链接时,我使用上面的 PHP header()
函数,但是当我检查服务器日志时,这似乎不起作用并给出 HTTP 200 OK< /code> 状态码。
这是服务器日志中的行:
[My IP] - - [12/Jun/2011:01:47:38 +0300] "GET /4k.html HTTP/1.1" 200 3284 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30"
4k.html
是不存在的链接。
我错过了什么吗?
I've built a dynamic website and modified the .htaccess file for static URLs and I need to create a 404 page for SEO.
header("HTTP/1.0 404 Not Found");
I'm using the above PHP header()
function when there is no matching link in database, but when I check the server logs it seems like this does not work and gives HTTP 200 OK
status code.
Here's the line from the server log:
[My IP] - - [12/Jun/2011:01:47:38 +0300] "GET /4k.html HTTP/1.1" 200 3284 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30"
4k.html
is the link which does not exist.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您将所有文件重写为单个 PHP 文件,以便 PHP 可以读取它并执行为页面提供服务所需的操作。这意味着 Apache 对 404 页面的处理本质上将被覆盖。此时,由您的 PHP 文件来提供页面或显示 404。据 Apache 所知,该请求导致 HTTP 代码为 200,因为它在触发 PHP 解析 404 页面后不会发挥任何作用。脚本并将输出提供给请求它的客户端。
在 PHP 脚本中,您将检查该页面是否存在。如果页面存在,则显示该页面,否则给出 404 消息。通过将
header("HTTP/1.0 404 Not Found", true);
放入 PHP 文件中您知道这是 404 的位置,它将向客户端发送 404 标头,因此它已正确完成。要验证是否正确发送,请触发 404(通过输入虚假网址)并在 Chrome 开发人员工具中查看请求详细信息,您应该在响应标头下看到您正在发送 404,而您的 Apache 日志显示 200 。From what I understand, you are rewriting all files to a single PHP file so PHP can read it and do what it needs to do to serve a page. That means that Apache's handling of 404 pages is going to be, essentially, overridden. At this point it is up to your PHP file to serve a page or display a 404. As far as Apache knows, the request resulted in an HTTP code of 200, because it doesn't play any part after it triggers PHP to parse the script and give the output to the client that requested it.
In your PHP script, you will be doing checking to see if the page exists or not. If the page exists, show the page, else, give a 404 message. By putting
header("HTTP/1.0 404 Not Found", true);
in your PHP file at the point where you know it's a 404, it will send the 404 header to the client, and therefore it was done correctly. To verify it is being sent correctly, trigger a 404 (by putting in a bogus url) and view the request details in Chrome Developer tools, you should see under Response headers that you are being sent a 404, while your Apache logs display a 200.