如何使用 PHP 创建错误 404 页面? [英] How to create an error 404 page using PHP?

查看:35
本文介绍了如何使用 PHP 创建错误 404 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件 .htaccess 处理从 /word_here 到我的内部端点 /page.php?name=word_here 的所有请求.PHP 脚本然后检查请求的页面是否在其页面数组中.

My file .htaccess handles all requests from /word_here to my internal endpoint /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages.

如果没有,我如何模拟 404 错误?

If not, how can I simulate an error 404?

我试过这个,但它没有导致我通过 ErrorDocument 配置的 404 页面出现在 .htaccess 中.

I tried this, but it didn't result in my 404 page configured via ErrorDocument in the .htaccess showing up.

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

我是否认为重定向到错误 404 页面是错误的?

Am I right in thinking that it's wrong to redirect to my error 404 page?

推荐答案

生成 404 页面的最新答案(从 PHP 5.4 或更高版本开始)是使用 http_response_code:

The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use http_response_code:

<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();

die() 不是绝对必要的,但它可以确保您不会继续正常执行.

die() is not strictly necessary, but it makes sure that you don't continue the normal execution.

这篇关于如何使用 PHP 创建错误 404 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆