I'm trying to use Apache's ErrorDocument
to handle client and server errors by passing them to error.php
.
.htaccess
ErrorDocument 400 /error.php
...
ErrorDocument 404 /error.php
...
ErrorDocument 511 /error.php
error.php
var_dump(http_response_code());
So, I point my browser to mywebsite.com/noeutdhoaeu
, which does not exist. The response from the server is 404 Not Found
, as you would expect. But PHP gives me 200
.
What gives?
Edit: I have this same exact code on my Apache-based localhost and it works just fine. That is the reason I am asking this question. PHP is completely aware that a 404 error has occurred in my local environment. On my hosted environment, however, PHP has no idea.
As @Boaz said, PHP doesn't know what Apache is going to set later.
You could use this approach to use the error code in PHP:
ErrorDocument 400 /error.php?error=400
ErrorDocument 401 /error.php?error=401
...
And in PHP test $_GET["error"]
.