If it’s PHP, you can try this:
ErrorDocument 403 http://localhost/pasta-do-meu-projeto/erro.php?status=403
ErrorDocument 404 http://localhost/pasta-do-meu-projeto/erro.php?status=404
ErrorDocument 501 http://localhost/pasta-do-meu-projeto/erro.php?status=501
And PHP:
<?php
if (isset($_GET['status'])) {
echo $_GET['status'];
}
Redirect vs mod_rewrite
If you do not want the URL to change when hitting an error page, but even then redirect occurs, you should use the RewriteCond combined with the RewriteRule, would look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /up/erro.php?status=404 [L]
However, if you want the url to look something like http://localhost/erro.php?status=404, so don’t use it, just use the ErrorDocument.
Rewriting without mod_rewrite
While doing some tests I noticed that
ErrorDocument 404 http://localhost/pasta-do-meu-projeto/erro.php?status=404
is different from
ErrorDocument 404 /pasta-do-meu-projeto/erro.php?status=404
When we use the first mode, Apache cannot detect for sure if it is a subdomain, or another domain or the same domain, so it doesn’t end up redirecting to the other page, also note that it is not possible to pick the variable REDIRECT_STATUS.
But be we use the second way with the path beginning by / the redirect is "internal", ie the page does not redirect, but the response comes from another location, which allows us to access the variables as REDIRECT_STATUS, note that it is not necessary to mod_rewrite qualified.
Updating
If you are using PHP5.4+ you can use the function http_response_code
Then the ErrorDocument you won’t need the ?status=
Reported: Get http status on PHP versions below 5.4
Ricardo, I discovered the reason for the URL redirect in some cases and in others not, I edited the answer.
– Guilherme Nascimento