You can get the error number returned by the page

1

I created a page (following the one taught in this page / a> error) for Error 404, but in this way it would have to make an error page for each HTTP Status, so I would like to do a webpage where it would be possible to get the possible HTTP header error and create the error page

ErrorDocument 403 http://localhost/pasta-do-meu-projeto/erro.html
ErrorDocument 404 http://localhost/pasta-do-meu-projeto/erro.html
ErrorDocument 501 http://localhost/pasta-do-meu-projeto/erro.html
    
asked by anonymous 22.04.2015 / 16:50

1 answer

2

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

    
22.04.2015 / 17:10