62

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.

John Smith
  • 8,567
  • 13
  • 51
  • 74
  • 1
    What does it do? Does it display Apache's 404 page? – Blender Apr 04 '11 at 03:23
  • "Someone said..." and you listened? – awm Apr 04 '11 at 03:24
  • 404.php displays my custom 404 page and HTTP/1.0 404 Not Found just does nothing. The php file just continues on like nothing happened. – John Smith Apr 04 '11 at 03:28
  • I believe ErrorDocument is only relevant when Apache doesn't know where to send a request. If it sends a request to a script, and that script choses to return a 404 header, apache will not invoke a second 404 script -- it will simply return the output of the initial script. – Frank Farmer Apr 04 '11 at 03:56
  • possible duplicate of [Why won't my PHP app send a 404 error?](http://stackoverflow.com/q/437256/952580) – Beetle Jan 11 '16 at 13:34

6 Answers6

104

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.\n";
die();
echo "Not after a die, however.\n";

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.
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • 1
    Is there any reason then why not just to do a single header('Location: 404.php'); call instead of doing that and calling HTTP/1.0 404 Not Found first? – John Smith Apr 04 '11 at 04:11
  • 7
    @John: If a file wasn't found, it's technically wrong to say "oh, I found that document; it's over here" when you're just redirecting to a page informing them that it wasn't found. To be semantically correct, you have to send the `HTTP/1.x 404 Not Found` and *not* redirect. – icktoofay Apr 04 '11 at 04:21
  • But that doesn't do anything..? Sorry if I'm missing something but doesn't the semantic version not work in my situation? – John Smith Apr 04 '11 at 04:51
  • 2
    @John: Well I was saying you should be 1. sending the HTTP thing, 2. including your `404.php` (not redirecting to it), and 3. calling `die` in that order. – icktoofay Apr 04 '11 at 04:57
  • 1
    Ah, I understand you now. That actually works quite well and since no redirect is required it is also very efficient! Thanks. – John Smith Apr 04 '11 at 05:43
34

You could try specifying an HTTP response code using an optional parameter:

header('HTTP/1.0 404 Not Found', true, 404);
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
20

Since PHP >= 5.4 you can use the following function to set the HTTP status code :

http_response_code(404);

you will still have the control over the content of the response but with the status code being 404 (you can have any status code you want).

http_response_code PHP Documentation

Avior
  • 481
  • 6
  • 18
10

Use these codes for 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)

Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
4

i think this will help you

content of .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

error.php and .htaccess should be put in the same directory [in this case]

Sourav
  • 17,065
  • 35
  • 101
  • 159
2

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.

Jeff_Alieffson
  • 2,672
  • 29
  • 34