'http_exception_template' => [
// 定义404错误的重定向页面地址
404 => APP_PATH.'404.html',
// 还可以定义其它的HTTP status
401 => APP_PATH.'401.html',
]
thinkphp 5.1
'http_exception_template' => [
// 定义404错误的模板文件地址
404 => Env::get('app_path') . '404.html',
// 还可以定义其它的HTTP status
401 => Env::get('app_path') . '401.html',
]
thinkphp 6.0
'http_exception_template' => [
// 定义404错误的模板文件地址
404 => \think\facade\App::getAppPath() . '404.html',
// 还可以定义其它的HTTP status
401 => \think\facade\App::getAppPath() . '401.html',
]
需要注意两点,一是只在部署模式下有效-即 app_debug 为 false 的时候。二是【APP_PATH.'404.html'】这个文件必须存在,一旦文件不存在路径不对,仍会是浏览器默认的404页面。
有时候,一些页面不想被某些用户访问到,比如定时发布文章,未发布之前访问文章链接应当是404页面,如果手动抛出404异常呢? 在thinkphp中很简单:
throw new \think\exception\HttpException(404, '页面不存在');
或者使用 abort 助手函数:
abort(404,'页面不存在');








