Enable php error page

4

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

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

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 ran the test with a nonexistent page on localhost!

Why does not apache redirect to error pages?

I also tried that in .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 have already restarted Apache and nothing

Setei AllowOverride for All on all occurrences of httpd.conf. I took the comment from the mod_rewrite line.

Nothing!

    
asked by anonymous 28.09.2015 / 16:00

1 answer

2

I think your problem resolves 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

In the meantime, I believe you are not aware of how to handle the occurrence of errors.

One of the ways to do error handling does not require a .htaccess redirect, simply implement a method through the language, some frameworks already include driver for error handling. But a simple way you could do to get around the problem is to create a method as in 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 the PHP Documentation

strong> that talks about error handling.

    
28.09.2015 / 16:35