如何在 PHP 中创建错误 404?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1381123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:17:18  来源:igfitidea点击:

How can I create an error 404 in PHP?

phpredirecthttp-status-code-404

提问by Eric

My .htaccess redirects all requests to /word_hereto /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages.

我的 .htaccess 将所有请求重定向/word_here/page.php?name=word_here. PHP 脚本然后检查请求的页面是否在其页面数组中。

If not, how can I simulate an error 404? I tried this, but it didn't result in my 404 page configured via ErrorDocumentin the .htaccessshowing up.

如果没有,我如何模拟错误 404?我想这一点,但它并不会导致我的404页通过配置ErrorDocument.htaccess显示出来。

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

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

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

回答by blade

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

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

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

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

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

回答by JW.

What you're doing will work, and the browser will receive a 404 code. What it won'tdo is display the "not found" page that you might be expecting, e.g.:

您所做的将起作用,浏览器将收到 404 代码。它不会做的是显示您可能期望的“未找到”页面,例如:

Not Found

未找到

The requested URL /test.php was not found on this server.

在此服务器上找不到请求的 URL /test.php。

That's because the web server doesn't send that page when PHP returns a 404 code (at least Apache doesn't). PHP is responsible for sending all its own output. So if you want a similar page, you'll have to send the HTML yourself, e.g.:

那是因为当 PHP 返回 404 代码时,Web 服务器不会发送该页面(至少 Apache 不会)。PHP 负责发送它自己的所有输出。所以如果你想要一个类似的页面,你必须自己发送 HTML,例如:

<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include("notFound.php");
?>

You could configure Apache to use the same page for its own 404 messages, by putting this in httpd.conf:

您可以将 Apache 配置为对其自己的 404 消息使用相同的页面,方法是将其放在 httpd.conf 中:

ErrorDocument 404 /notFound.php

回答by Ates Goral

Try this:

尝试这个:

<?php
header("HTTP/1.0 404 Not Found");
?>

回答by Irshad Khan

Create custom error pages through .htaccess file

通过 .htaccess 文件创建自定义错误页面

1. 404 - page not found

1. 404 - 页面未找到

 RewriteEngine On
 ErrorDocument 404 /404.html

2. 500 - Internal Server Error

2. 500 - 内部服务器错误

RewriteEngine On
ErrorDocument 500 /500.html

3. 403 - Forbidden

3. 403 - 禁止

RewriteEngine On
ErrorDocument 403 /403.html

4. 400 - Bad request

4. 400 - 错误的请求

RewriteEngine On
ErrorDocument 400 /400.html

5. 401 - Authorization Required

5. 401 - 需要授权

RewriteEngine On
ErrorDocument 401 /401.html

You can also redirect all error to single page. like

您还可以将所有错误重定向到单个页面。喜欢

RewriteEngine On
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
ErrorDocument 403 /404.html
ErrorDocument 400 /404.html
ErrorDocument 401 /401.html

回答by Eli

Did you remember to die() after sending the header? The 404 header doesn't automatically stop processing, so it may appear not to have done anything if there is further processing happening.

你还记得在发送标头后 die() 吗?404 标头不会自动停止处理,因此如果有进一步的处理发生,它可能看起来没有做任何事情。

It's not good to REDIRECT to your 404 page, but you can INCLUDE the content from it with no problem. That way, you have a page that properly sends a 404 status from the correct URL, but it also has your "what are you looking for?" page for the human reader.

重定向到您的 404 页面并不好,但您可以毫无问题地包含其中的内容。这样,您的页面就可以从正确的 URL 正确发送 404 状态,但它也包含您的“您在寻找什么?” 人类读者的页面。

回答by the red crafteryt

try putting

尝试放置

ErrorDocument 404 /(root directory)/(error file) 

in .htaccessfile.

.htaccess文件中。

Do this for any error but substitute 404 for your error.

对任何错误执行此操作,但将 404 替换为您的错误。

回答by Mike Godin

In the Drupal or Wordpress CMS (and likely others), if you are trying to make some custom php code appear not to exist (unless some condition is met), the following works well by making the CMS's 404 handler take over:

在 Drupal 或 Wordpress CMS(以及其他可能的)中,如果您试图使某些自定义 php 代码看起来不存在(除非满足某些条件),通过让 CMS 的 404 处理程序接管以下工作,效果很好:

<?php
  if(condition){
    do stuff;
  } else {
    include('index.php');
  }
?>

回答by Mani Kandan

try this once.

试试这个。

$wp_query->set_404();
status_header(404);
get_template_part('404'); 

回答by user5891645

Immediately after that line try closing the response using exitor die()

在该行之后立即尝试使用exit或关闭响应die()

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

or

或者

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