软404错误:PHP没有发送404标头

时间:2017-11-15 16:43:08

标签: php apache http-status-code-404

我有一个PHP脚本,当在数据库中找不到页面时,我发送http标头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错误,因为这很奇怪?

enter image description here

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

enter image description here

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

1 个答案:

答案 0 :(得分:1)

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()功能。

相关问题