.htaccess show 404, 403, 500, error pages via PHP

后端 未结 3 1746
小鲜肉
小鲜肉 2020-12-22 06:26

How can I make .htaccess display errors from a PHP file? I mean when I search a non-existing file .htaccess should show me an error page from error.php, but error.php needs

3条回答
  •  萌比男神i
    2020-12-22 07:05

    You're looking for ErrorDocument.

    In your .htaccess specify the codes you want to handle, like:

    ErrorDocument 403 /error.php
    ErrorDocument 404 /error.php
    ErrorDocument 500 /error.php
    

    And in error.php, handle the error codes like:

     'Forbidden',
            404 => 'Not Found',
            500 => 'Internal Server Error'
        );
        $source_url = 'http'.((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 's' : '').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        if (array_key_exists($code, $codes) && is_numeric($code)) {
            die("Error $code: {$codes[$code]}");
        } else {
            die('Unknown error');
        }
    ?>
    

提交回复
热议问题