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

查看:45
本文介绍了.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 应直接在当前 url 上显示错误,而无需重定向.我可以这样做还是不可能?还有其他方法吗?

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天全站免登陆