du131642 2014-09-16 13:01
浏览 49
已采纳

.htaccess通过PHP显示404,403,500,错误页面

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 a parameter with the error code.

NOTE: .htaccess should show the error directly on the current url without redirecting. Can I do this or it is impossible? Are there any other ways?

  • 写回答

3条回答 默认 最新

  • dongshi9719 2014-09-16 13:13
    关注

    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:

    <?php
        $code = $_SERVER['REDIRECT_STATUS'];
        $codes = array(
            403 => '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');
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!