Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

php - header('HTTP/1.0 404 Not Found'); not doing anything

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.

I tried different variations including ErrorDocument 404 404.php and ErrorDocument 404 mywebite/404.php but to no avail.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, it probably is actually working. It's just not readily visible. Instead of just using the header call, try doing that, then including 404.php, and then calling 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:

<?php

header("HTTP/1.0 404 Not Found");
echo "PHP continues.
";
die();
echo "Not after a die, however.
";

Then viewing the result with curl -D /dev/stdout reveals:

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...