.htaccess通过PHP显示404、403、500,错误页面 [英] .htaccess show 404, 403, 500, error pages via PHP

查看:136
本文介绍了.htaccess通过PHP显示404、403、500,错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使PHP文件中的.htaccess显示错误?我的意思是,当我搜索不存在的文件时,.htaccess应该会向我显示error.php的错误页面,但是error.php需要带有错误代码的参数。

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.

注意: :. htaccess应该直接在当前网址上显示错误,而不进行重定向。我可以这样做还是不可能?还有其他方法吗?

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?

推荐答案

您正在寻找 ErrorDocument

在.htaccess中指定要处理的代码,例如:

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

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

然后在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');
    }
?>

这篇关于.htaccess通过PHP显示404、403、500,错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆