thinkphp添加404页面及apache 404页面设置方法

发布时间:2019-07-25编辑:脚本学堂
有关thinkphp中添加404页面的方法,通过直接定义空模块和空操作实现404跳转,并注意header头信息的设置,在网站配置中加入 ErrorDocument 404 /404.html 即可。

thinkphp提供了404页面的处理机制,只需要在lib下定义一个EmptyAction.class.php,且实现以下方法即可。

代码:
 

复制代码 代码示例:
<?php
class EmptyController extends Controller {
function _empty(){
header("HTTP/1.0 404 Not Found");
$this->display('Public:404');
}
// 404
function index() {
header("HTTP/1.0 404 Not Found");
$this->display('Public:404');
}
}
?>

以上通过直接定义空模块和空操作实现404跳转。

注意:
设置header头很重要, 不然的话返回的状态会是200.
该类对应Public模板目录下需要有名为404的页面模板.

apache中设置:
在网站配置中加入 ErrorDocument 404 /404.html 即可。