404 Error Header
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads return 404 error php with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack header( http/1.0 404 not found ) Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign 404 header example up How can I create an error 404 in PHP? up vote 26 down vote favorite 6 My .htaccess redirects all requests to /word_here to /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages. If php header location 301 not, how can I simulate an error 404? I tried this, but it didn't work: header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); Am I right in thinking that it's wrong to redirect to my error 404 page? php redirect http-status-code-404 share|improve this question edited May 9 '10 at 21:27 Gumbo 393k63541666 asked Sep 4 '09 at 19:29 Eric 50.8k24114223 Why do you mean it doesn’t work? Did you check the server response header? –Gumbo Sep 4 '09 at 19:32 1 In answer to
Php Plain Text Header
your concluding question, yes, it would be a bad idea to redirect all 404s to a real page. This violates the HTTP spec by turning something that shouldn't be there into something that is there. –Lucas Oman Sep 4 '09 at 19:41 add a comment| 4 Answers 4 active oldest votes up vote 49 down vote accepted What you're doing will work, and the browser will receive a 404 code. What it won't do is display the "not found" page that you might be expecting, e.g.: Not Found The requested URL /test.php was not found on this server. 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.: You could configure Apache to use the same page for its own 404 messages, by putting this in httpd.conf: ErrorDocument 404 /notFound.php share|improve this answer edited Mar 23 at 14:47 Kzqai 11.3k1678109 answered Sep 4 '09 at 19:50 JW. 28.4k207999 Thanks. I'd assumed it used my 404 page. –Eric Sep 4 '09 at 20:17 @JW where the above code will be use ? is that code for '.htaccess' file. –Manohar Kumar Feb 20 '15 at 6:13 add a comment| up vote 9 down vote Try this: share|improve this a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of
Php 404 Page
this site About Us Learn more about Stack Overflow the company Business php 404 error page redirect Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask php 404 redirect htaccess Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up http://stackoverflow.com/questions/1381123/how-can-i-create-an-error-404-in-php Why won't my PHP app send a 404 error? up vote 135 down vote favorite 24 if (strstr($_SERVER['REQUEST_URI'],'index.php')) { header('HTTP/1.0 404 Not Found'); } Why wont this work? I get a blank page. php header http-status-code-404 share|improve this question edited Jan 11 '14 at 23:29 danorton 6,24832644 asked Jan 12 '09 at 22:26 Yrrol 1 Better option would be to send a http://stackoverflow.com/questions/437256/why-wont-my-php-app-send-a-404-error 404 status and include your custom 404 page immediately. That's what i used to do. –Rocky Oct 25 '12 at 7:05 On my PHP server it works perfectly. The above line returns the server's default 404 page. I suppose that this can be configured in the server. –Elmue Oct 7 '14 at 23:25 add a comment| 16 Answers 16 active oldest votes up vote 297 down vote Your code is technically correct. If you looked at the headers of that blank page, you'd see a 404 header, and other computers/programs would be able to correctly identify the response as file not found. Of course, your users are still SOL. Normally, 404s are handled by the web server. User: Hey, do you have anything for me at this URI webserver? Webserver: No, I don't, 404! Here's a page to display for 404s. The problem is, once the web server starts processing the PHP page, it's already passed the point where it would handle a 404 User: Hey, do you have anything for me at this URI webserver? Webserver: Yes, I do, it's a PHP page.
that will work. get httpfox for firefox and you can check the return codes. Zaggs 2008-12-04 https://www.sitepoint.com/community/t/php-force-404-error/4490 21:51:53 UTC #3 galen said: that will work. get httpfox for firefox and you can check the return codes. Ok, but it's not forcing a redirect to the 404 https://en.wikipedia.org/wiki/HTTP_404 page, any ideas? Zaggs 2008-12-05 10:57:38 UTC #4 still struggling with this? would greatly appreciate any help... Stormrider 2008-12-05 11:02:32 UTC #5 All it does its return the status 404 error code, it doesn't force a redirect to any 404 page or anything. You have to do that yourself. Zaggs 2008-12-05 11:09:48 UTC #6 Stormrider said: All it does its return the status code, it doesn't force a redirect to any 404 page or anything. You have to do that yourself. How would I redirect to 404.shtml? Stormrider 2008-12-05 11:20:41 404 error header UTC #7 Best way is probably just to include() it after sending the status code, then exit(). galen 2008-12-05 16:12:18 UTC #8 try ErrorDocument 404 /notfound.html in .htaccess Stormrider 2008-12-05 18:00:47 UTC #9 That will work for apache generated 404 errors. For ones you throw in php, you will need to output the page yourself. felgall 2008-12-05 18:30:39 UTC #10 The easiest way would be: header("Location: dkjlflkjxfgkdnbcxfn.html"); As long as you don't actually have a page with that filename. Stormrider 2008-12-05 22:45:49 UTC #11 Then there is little point in outputting the 404 header, since that would generate the error anyway. Would look a bit rubbish though. imaginethis 2008-12-05 23:09:43 UTC #12 Did you try header("HTTP/1.0 404 Not Found"); echo "
404 File not found
"; ? er... ignore me... I'm half reading... half working >.> you can also try header("Status: 404 Not Found"); echo "404 File not found
"; system 2014-10-08 00:47:58 UTC #13 Home Categories FAQ/Guidelines Terms of Service Privacy Policy Powered by Discourse, best viewed with JavaScript enabled Shop Versioning Reference Articles PremiumStatus codes 301 Moved Permanently 302 Found 303 See Other 403 Forbidden 404 Not Found 451 Unavailable For Legal Reasons v t e The 404 or Not Found error message is a Hypertext Transfer Protocol (HTTP) standard response code, in computer network communications, to indicate that the client was able to communicate with a given server, but the server could not find what was requested. The web site hosting server will typically generate a "404 Not Found" web page when a user attempts to follow a broken or dead link; hence the 404 error is one of the most recognizable errors encountered on the World Wide Web. Contents 1 Overview 2 Custom error pages 2.1 Tracking/Checking 404 errors 3 Phony 404 errors 4 404 substatus error codes defined by IIS 4.1 Slang usage 5 See also 6 References 7 External links Overview[edit] When communicating via HTTP, a server is required to respond to a request, such as a web browser request for a web page, with a numeric response code and an optional, mandatory, or disallowed (based upon the status code) message. In the code 404, the first digit indicates a client error, such as a mistyped Uniform Resource Locator (URL). The following two digits indicate the specific error encountered. HTTP's use of three-digit codes is similar to the use of such codes in earlier protocols such as FTP and NNTP. At the HTTP level, a 404 response code is followed by a human-readable "reason phrase". The HTTP specification suggests the phrase "Not Found"[1] and many web servers by default issue an HTML page that includes both the 404 code and the "Not Found" phrase. A 404 error is often returned when pages have been moved or deleted. In the first case, it is better to employ URL mapping or URL redirection by returning a 301 Moved Permanently response, which can be configured in most server configuration files, or through URL rewriting; in the second case, a 410 Gone should be returned. Because these two options require special server configuration, most websites do not make use of them. 404 errors should not be confused with DNS errors, which appear when the given URL refers to a server name that does not exist. A 404 error indicates that the server itself was found, but that the server was not able to retrieve the requested page. Custom error pages[edit] The Wikimedia 404 message Web servers can typically be config