[英].htaccess show 404, 403, 500, error pages via PHP
如何使PHP文件中的.htaccess显示错误? 我的意思是,当我搜索不存在的文件时,.htaccess应该会向我显示error.php的错误页面,但是error.php需要带有错误代码的参数。
注意: .htaccess应该直接在当前URL上显示错误,而不进行重定向。 我可以这样做还是不可能? 还有其他方法吗?
您正在寻找ErrorDocument
。
在.htaccess中,指定要处理的代码,例如:
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
在error.php中,处理以下错误代码:
<?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');
}
?>
//Custom 403 errors
ErrorDocument 403 your-path/403.php
//Custom 404 errors
ErrorDocument 404 your-path/404.php
//Custom 500 errors
ErrorDocument 500 your-path/500.php
当您在.htacess中引用错误页面时,您所做的只是重定向:
ErrorDocument 404 /404.htm
将其更改为error.php?code=404
,然后使用以下命令在error.php
进行选择:
if($_GET['code'] == '404') {
include('404.php');
}
瞧!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.