Why doesn't apache show a 404 error when I send a 404 header with php?

橙三吉。 提交于 2019-12-01 16:53:24

问题


I have a header('HTTP/1.0 404 Not Found'); somewhere along the code but it doesn't redirect to the Apache's default 404 page for some reason.

I have a Rewrite Rule on the .htaccess file that redirects every request to index.php. Could that be the problem?


回答1:


The header is not what tells Apache to display it's 404 page. Rather, when Apache displays its 404 page, it sends a 404 header along with it. The header is meant to have meaning to the browser, not the server. Apache displays a 404 when it can't find the proper file to display. Since you're in a PHP script, Apache has already found a file it can display, and thus won't show its own 404 page.




回答2:


Headers sent by PHP only matter really to the browser in this case. Apache isn't going to make its own page because you are already processing the page, and if you sent something, the two would conflict.

Yes, the .htaccess file is going to stop Apache from showing an error page because your rules makes Apache think it no longer has a 404 error, because it has found a page to show.

Sending a header is really only a 'status message', and doesn't make the browser or server show a particular page. (Although most browsers will).

As Dav pointed out in the comments, you will want to send 404 errors to their own custom error page.



来源:https://stackoverflow.com/questions/1247352/why-doesnt-apache-show-a-404-error-when-i-send-a-404-header-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!