It is possible to get the Error Number returned by Page

Asked

Viewed 117 times

1

I created a page (following the one taught in this page ) error for Error 404, but this way would have to make an error page for each HTTP Status, then Try to make a gnerica where it was possible to catch the possible http header error and create based on the error page, ai when you were setting the error pages in apache would look like this:

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
  • Ricardo, I discovered the reason for the URL redirect in some cases and in others not, I edited the answer.

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 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

  • .@Guilhermenascimento obliged to discover was just the second form he was using, if he could give upvote again in his reply.

  • 1

    @Ricardohenrique For nothing, believe that I went to create my url-rewritten and started to catch, kkkk, now I’ve been able to discover several things, only with difficulty to do the same effect on Nignix and Lighttpd (apache type servers)if I get everything put here to share with the staff.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.