繁体   English   中英

404错误页面显示HTTP状态200而不是404

[英]404 error page showing HTTP Status 200 instead of 404

我有以下用于执行重定向的代码。 如果我的$includeFile不存在,它将重定向到404页面。 这是我的index.php的片段

if ( !file_exists($includeFile) )
     Header( "HTTP/1.1 404 Page Not Found" );
     Header( "Location: http://legascy.com/404_error/");

但是http://legascy.com/404_error/显示的是HTTP状态200,而不是404。

如何解决呢?

您需要将404的标头函数放在“ 404_error”页面上,而不是重定向页面上。 这是因为浏览器被发送到404_error,它具有默认的200状态代码。

if ( !file_exists($includeFile) ) {
     header("Location: http://legascy.com/404_error/");
}

并在404_error / index.php上:

header("HTTP/1.1 404 Page Not Found");

您之前发送404错误代码的标头已被Location标头丢弃,浏览器将用户带到新页面。 重定向到的页面可用,因此它将始终显示200。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM