重定向到所有错误的特定页面(404,405等)

时间:2015-06-29 06:17:29

标签: php .htaccess redirect

我已经考虑了很长一段时间,但实际上没有时间对它进行一些研究,但现在这已经让我不得不作为一种要求,

我想知道;有没有办法将所有错误重定向到PHP中的特定页面,例如我知道,.htaccess文件可用于此目的,只要有404错误,它会自动重定向到某个错误页面或有趣的404页面,

我想知道是否有其他方法可以重定向所有其他错误,例如php脚本错误和真实性错误等。

2 个答案:

答案 0 :(得分:1)

您可以将php脚本用作错误文档。在.htaccess中使用此语法:

ErrorDocument 500 /your-file.php

在php文件中,您可以使用$_SERVER['REDIRECT_STATUS']

<?php

$status=$_SERVER['REDIRECT_STATUS'];
$codes=array(
      400 => array('400 Bad Request', 
                   'The request cannot be fulfilled due to bad syntax.'),
      401 => array('401 Login Error', 
                   'It appears that the password and/or user-name you entered was incorrect.'),
      500 => array('500 Internal Server Error',
                   'The request was unsuccessful due to an unexpected condition encountered by the server.'),
      //all the other necessary codes
);

if(isset($codes[$status])){
    $errortitle=$codes[$status][0];
    $message=$codes[$status][1];
}
else{
    $errortitle="Unknown Error";
    $message="An unknown error has occurred. Code: $status.";
}
?>

<!doctype html>
<html>
<head>
<title><?php echo("$errortitle");?></title>
<meta charset="utf-8">
</head>
<body>

<?php
echo('<h1>'.$errortitle.'</h1>');
echo('<p>'.$message.'</p>');
?>

</body>
</html>

答案 1 :(得分:1)

您可以使用此:

header("405",true,405);

重定向HTTP错误405

相关问题