php header('HTTP/1.0 404 未找到'); 什么都不做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5534268/
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
header('HTTP/1.0 404 Not Found'); not doing anything
提问by John Smith
I have a 404.php file in my site's main directory and I was using header('Location: 404.php');
for a while until someone said that you should use header('HTTP/1.0 404 Not Found');
instead. So I replaced it with that and then added: ErrorDocument 404 /404.php
to my apache config file and restarted the server but it doesn't work.
我的站点的主目录中有一个 404.php 文件,我使用header('Location: 404.php');
了一段时间,直到有人说你应该使用它header('HTTP/1.0 404 Not Found');
。所以我用它替换了它,然后添加:ErrorDocument 404 /404.php
到我的 apache 配置文件并重新启动服务器,但它不起作用。
I tried different variations including ErrorDocument 404 404.php
and ErrorDocument 404 mywebite/404.php
but to no avail.
我尝试了不同的变体,包括ErrorDocument 404 404.php
和ErrorDocument 404 mywebite/404.php
但无济于事。
What I mean by doesn't work is that earlier when using header('Location: 404.php');
it would redirect to the 404.php file but when I replace it with header('HTTP/1.0 404 Not Found');
it seems to just skip over the line and not do anything. It most certainly is not redirecting. The reason I am calling for the redirect is because if a $_GET header value is not recognized the page should 404.
我的意思是不工作是早期使用header('Location: 404.php');
它时会重定向到 404.php 文件,但是当我用它替换它时,header('HTTP/1.0 404 Not Found');
它似乎只是跳过该行而不做任何事情。它肯定不是重定向。我要求重定向的原因是因为如果 $_GET 标头值不被识别,页面应该是 404。
回答by icktoofay
No, it probably is actually working. It's just not readily visible. Instead of justusing the header
call, try doing that, then including 404.php
, and then calling die
.
不,它可能确实有效。它只是不容易看到。不要只使用header
调用,而是尝试这样做,然后包含404.php
,然后调用die
.
You can test the fact that the HTTP/1.0 404 Not Found
works by creating a PHP file named, say, test.php
with this content:
您可以HTTP/1.0 404 Not Found
通过创建一个名为的 PHP 文件来测试它是否有效,例如,test.php
包含以下内容:
<?php
header("HTTP/1.0 404 Not Found");
echo "PHP continues.\n";
die();
echo "Not after a die, however.\n";
Then viewing the result with curl -D /dev/stdout
reveals:
然后查看结果curl -D /dev/stdout
显示:
HTTP/1.0 404 Not Found
Date: Mon, 04 Apr 2011 03:39:06 GMT
Server: Apache
X-Powered-By: PHP/5.3.2
Content-Length: 14
Connection: close
Content-Type: text/html
PHP continues.
回答by Jimmy Sawczuk
You could try specifying an HTTP response code using an optional parameter:
您可以尝试使用可选参数指定 HTTP 响应代码:
header('HTTP/1.0 404 Not Found', true, 404);
回答by Shiv Singh
Use these codes for 404 not found.
对 404 not found 使用这些代码。
if(strstr($_SERVER['REQUEST_URI'],'index.php')){
header('HTTP/1.0 404 Not Found');
readfile('404missing.html');
exit();
}
Here 404missing.html is your Not found design page. (it can be .html or .php)
这里 404missing.html 是你的 Not found 设计页面。(可以是 .html 或 .php)
回答by Sourav
i think this will help you
我想这会帮助你
content of .htaccess
.htaccess 的内容
ErrorDocument 404 /error.php
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 405 /error.php
ErrorDocument 406 /error.php
ErrorDocument 409 /error.php
ErrorDocument 413 /error.php
ErrorDocument 414 /error.php
ErrorDocument 500 /error.php
ErrorDocument 501 /error.php
ErrorDocument 404 /error.php
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 405 /error.php
ErrorDocument 406 /error.php
ErrorDocument 409 /error.php
ErrorDocument 413 /error.php
ErrorDocument 414 /error.php
ErrorDocument 500 /error.php
ErrorDocument 501 /error.php
error.php and .htaccess should be put in the same directory [in this case]
error.php 和 .htaccess 应该放在同一个目录中[在这种情况下]
回答by Avior
For PHP >= 5.4 you can use a simpler function like this : http_response_code(404);
PHP Documentation
对于 PHP >= 5.4,您可以使用更简单的函数,如下所示:http_response_code(404);
PHP 文档
回答by Jeff_Alieffson
Another reason may be if you add any html tag before this redirect. Look carefully, you may left DOCTYPE or any html comment before this line.
另一个原因可能是您在此重定向之前添加了任何 html 标记。仔细看,你可以在这行之前留下 DOCTYPE 或任何 html 注释。
回答by Jeff_Alieffson
After writing
写完之后
header('HTTP/1.0 404 Not Found');
add one more header for any inexisting page on your site. It works, for sure.
为您网站上的任何现有页面添加一个标题。它确实有效。
header("Location: http://yoursite/nowhere");
die;