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 an error page hits, but redirection still occurs, you should use RewriteCond
combined with RewriteRule
, it 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 be something like http://localhost/erro.php?status=404
, then do not use it, just use ErrorDocument
.
Rewrite 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 can not detect for sure if it is a subdomain, or another domain or the same domain, then it does not happen that redirects to the other page, also note that it is not possible to get the variable
REDIRECT_STATUS
.
However, to be able to use the second mode with the path starting with /
the redirection 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 you do not need mod_rewrite
enabled.
Update
If you are using PHP5.4 + you can use the http_response_code
Then ErrorDocument
will not need ?status=
Reported: Get http status in PHP versions below 5.4