Soft 404 error: PHP not sending 404 header

Soft 404 error: PHP not sending 404 header

当在数据库中找不到页面时,我有一个 php 脚本,我发送 http headers 404 not found,而不是 php 发送 HTTP 200。Google 网站管理员工具显示其软 404 错误。

<?php 
if (mysql_num_rows($rs_press) == 0) {

    header("HTTP/1.1 404 Not Found");
    include('404.shtml');
    exit;

}
?>

并且 GMT 页面的 HTTP 响应非常奇怪,显示 HTTP 200 OK。为什么 PHP 发送 200 而不是 404 错误,因为这很奇怪?。

if 代码工作正常,但只有 404 headers 未发送。这是页面的输出..

我该如何解决? 我在专用服务器上 运行 PHP 5.6/Apache,已激活 SSL 的 Smarty.

PHP header() function 实际上有 3 个参数:

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

尝试将您的代码更改为:

<?php 
if (mysql_num_rows($rs_press) == 0) {

    header("HTTP/1.1 404 Not Found", true, 404);
    include('404.shtml');
    exit;

}
?>

或者,您可以尝试使用 http_response_code() 函数。