Enable php error page

Asked

Viewed 562 times

4

I’ve seen several attempts to enable error pages in apache/php but none worked here.

have file .htaccess and in it I have the following lines:

ErrorDocument 400 /index.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(index|index/)$ index.php [NC,L]
    RewriteRule ^index/([a-z0-9-]+)$ index.php?pagina=$1 [NC]
</IfModule>

I tested with a missing page on localhost!

Why apache does not redirect to error pages?

Also tried so on . htaccess and did not give

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    ErrorDocument 400 /index.php
    ErrorDocument 401 /401.php
    ErrorDocument 403 /403.php
    ErrorDocument 404 /404.php
    ErrorDocument 500 /500.php

    RewriteRule ^(index|index\/)$ index.php [NC,L]
    RewriteRule ^index\/([a-z0-9-]+)$ index.php?pagina=$1 [NC]
</IfModule>

I’ve reestablished Apache and nothing

Setei Allowoverride para All in all httpd.conf occurrences. I removed the comment from the mod_rewrite line.

Nothingness!

  • You’re putting it out of the mod_rewrite tag, it’ll never work. And you’re putting it before Rewriteengine On, which means you’re not using it in rewrite mode.

1 answer

2

I believe your problem is solved like this:

    RewriteEngine On
    RewriteBase /
    ErrorDocument 401 /401.php
    ErrorDocument 403 /403.php
    ErrorDocument 404 /404.php
    ErrorDocument 500 /500.php

    RewriteRule ^(index|index\/)$ index.php [NC,L]
    RewriteRule ^index\/([a-z0-9-]+)$ index.php?pagina=$1 [NC]

But you can instead of putting ErrorDocument, use only one redirect:

Redirect 401 /401.php
Redirect 403 /403.php
Redirect 404 /404.php
Redirect 500 /500.php

However, I believe you don’t know how to handle error exit.

One of the ways to make error handling does not require a redirect by .htaccess, just implement a method through the language, some Frameworks already include controller for error handling. But a simple way that you could do to get around the problem is to create a method as the example below:

<?php

    function errorReportingHandler($code = NULL) {
            $return = array(401, 403, 404, 500);
            if ($code !== NULL) {
               if (in_array($code, $return)) {
                   header('Location: ' . $return[$code].'.php');
               } else {
                   header('Location: default_errors.php');
               }
            }
     }
     $GLOBALS['http_response_code'] = $code;
     errorReportingHandler($code);
?>

For more information, I suggest accessing PHP documentation who talks about dealing with errors.

  • It didn’t work. Believe it? It will be in the file . htaccess

  • I think it worked. But I can’t transfer to 404.php that you don’t think even though the page is there. Need to have .html. Some correction on this?

  • Wow. Now instead of opening the index.html page which is the new error page it is displaying it Works!

  • I found out. The problem is that you need to pass the full address of the error page. Type http://..... Is there a rule to pass only the relative path? E will there be a way to redirect to and from page 404.php, address of address bar remain the same and page 404;php be included?

  • I’ll edit the answer, it should solve your problem.

  • When editing says here that you edited it because I just tested it and it hasn’t worked yet!

  • I’ve already edited @Carlosrocha. See if the solution solves your problem.

Show 2 more comments

Browser other questions tagged

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