PHP - 显示 404 错误而不重定向到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11137625/
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
PHP - Display a 404 Error without redirecting to another page
提问by Dilip Raj Baral
I want to display a 404 Error if a user reaches a page that exists but I don't want him/her to see.
如果用户到达一个存在但我不希望他/她看到的页面,我想显示 404 错误。
I don't want to do redirect (that would cause the address bar to show the link of the error page in the address bar) like the following:
我不想做重定向(这会导致地址栏在地址栏中显示错误页面的链接),如下所示:
if ($this_page_should_not_be_seen)
header("Location: err.php?e=404");
Instead, it should seem like the page really doesn't exist, without having the URL in the browser's address changed.
相反,如果浏览器地址中的 URL 没有改变,页面看起来应该真的不存在。
回答by Emil Vikstr?m
Include the error page in your current page and send a 404 error status code:
在当前页面中包含错误页面并发送 404 错误状态代码:
<?php
if ($nobody_should_ever_be_here) {
header('HTTP/1.1 404 Not Found'); //This may be put inside err.php instead
$_GET['e'] = 404; //Set the variable for the error code (you cannot have a
// querystring in an include directive).
include 'err.php';
exit; //Do not do any more work in this script.
}
?>
Note that this should be used if the page should never be seen. A better status code for un-authorized access (if the page should be seen by some logged in users) is 403 (Not Authorized).
请注意,如果永远不应该看到页面,则应使用此选项。未经授权访问的更好状态代码(如果某些登录用户应该看到该页面)是 403(未授权)。
回答by Jon
You can achieve a 404 (or any other HTTP response code really) programmatically with
您可以通过编程方式实现 404(或任何其他 HTTP 响应代码)
header('HTTP/1.0 404 Not Found');
die;
回答by Mojtaba Rezaeian
In my experience the best way to show 404 error document without changing URL is to define error document in apaches .htaccessfile:
根据我的经验,在不更改 URL 的情况下显示 404 错误文档的最佳方法是在 apaches.htaccess文件中定义错误文档:
RewriteEngine on
RewriteBase /
# Defines 404 error pages content:
ErrorDocument 404 /my_root/errors/404.html
# for all invalid links (non existing files):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L,R=404]
# for some valid links (existing files to be un-accessible):
RewriteCond %{THE_REQUEST} ^.*some_file.php.*$ [NC]
RewriteRule .* - [L,R=404]
回答by OptimusCrime
What you write makes it hard to us to understand.
你写的东西让我们很难理解。
I want to redirect any incoming user to the 404 Error Page if s/he reaches the page I don't want him/her to reach.
如果他/她到达我不希望他/她到达的页面,我想将任何传入用户重定向到 404 错误页面。
So, a person reaches a page that exists?
那么,一个人到达一个存在的页面?
If the person reaches for example a protected page that he/she is not supposted to see. Using header is the best way. Your options are to echo meta-refresh or javascript, but header is much cleaner. You could display something like You do not have permission to do that!which is pretty common on the web. If you don't want to redirect you could display a 404 "fake message" via the header.
例如,如果此人到达他/她不应该看到的受保护页面。使用标题是最好的方法。您可以选择 echo meta-refresh 或 javascript,但标题要干净得多。您可以显示类似You do not have permission to do that!网络上很常见的内容。如果您不想重定向,您可以通过标题显示 404“假消息”。
If you are talking about someone reaching a 404 page, a file that does not exists, you only option is to use .htaccess
如果你正在谈论某人到达 404 页面,一个不存在的文件,你唯一的选择是使用 .htaccess
回答by Sendy
may you can direct to your 404 page like
您可以直接访问您的 404 页面,例如
header('Location: http://www.website.com/errors/404.php');
header('位置:http: //www.website.com/errors/404.php');

