Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 997
the_drow Avatar asked Aug 07 '09 22:08

the_drow


People also ask

Why Apache server does not work error 404?

Error 404 not found is one of the most common issues you may encounter while browsing. This HTTP status code means the requested page can't be found on the website server. It may indicate a flaw with a hosting service or your domain name system (DNS) configuration settings.

Can a 404 page be PHP?

php file, you can create one as a simple HTML file with a . php extension and then upload it to your site's theme directory. Any time a 404 error occurs, WordPress will serve up this 404. php page to the user.

What causes a 404 Not Found error?

404 error codes are generated when a user attempts to access a webpage that does not exist, has been moved, or has a dead or broken link. The 404 error code is one of the most frequent errors a web user encounters. Servers are required to respond to client requests, such as when a user attempts to visit a webpage.


2 Answers

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.

like image 159
Amber Avatar answered Sep 20 '22 16:09

Amber


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.

like image 38
Tyler Carter Avatar answered Sep 16 '22 16:09

Tyler Carter