Szymon Toda
Szymon Toda

Reputation: 4516

PHP 404 handling

All requests go through index.php, and if content was not found I do:

header("HTTP/1.0 404 Not Found");
exit;

and expect to see contents of /404 as defined in .htaccess: ErrorDocument 404 /404

The problem is that I see a blank page on Chrome and Firefox, but on IE see its 404 page (not mine, browsers 404 page).

Sending header is not enough to handle redirect, so it's expected to be done by .htaccess, but fails. Should I redirect it whith PHP like so:

header("HTTP/1.0 404 Not Found");
header("Location: " . $dirpath . "404"); 

Upvotes: 1

Views: 868

Answers (1)

anubhava
anubhava

Reputation: 786081

No ErrorDocument 404 won't work on your way out from PHP. That is only applicable when Apache detects 404 for an incoming request and ends up invoking ErrorDocument 404 handler.

Once control is handed over to PHP as normal request processor Apache just returns output returned by PHP module to a requesting client.

Only thing you can do is this:

require_once("404.php"); // include 404 handler
exit;

And inside 404.php you can do:

http_response_code(404); // sends 404 status to browser

Upvotes: 1

Related Questions