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

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

I have a Rewrite rule in a .htaccess file that redirects every request to index.php. Could this be a problem?

+7
php apache mod-rewrite
source share
2 answers

The title is not what Apache says to display on page 404. Rather, when Apache displays page 404, it sends the title 404 with it. The title should make sense for the browser, not the server. Apache displays 404 when it cannot find the file to display. Since you are in a PHP script, Apache has already found a file that it can display, and therefore will not display its own 404 page.

+18
source share

The headers sent by PHP in this case are only relevant to the browser. Apache is not going to create its own page, because you are already processing the page, and if you send something, they conflict.

Yes, the .htaccess file will prevent Apache from showing the error page because your rules cause Apache to no longer have the 404 error because it has detected that the page is being displayed.

Sending a header is actually just a "status message" and does not make the browser or server display a specific page. (Although most browsers will be).

As Dove noted in the comments, you'll want to send 404 errors to your own error page.

+7
source share

All Articles