0

In my website, website.com/downloads actually goes to website.com/index.php?page=downloads (with apache rewriting). I have a custom 404 not found error page set. If I do website.com/dkfjf/fdf, It goes to the 404 error, but If I do website.com/something, it goes to index.php?page=something, so then in my index.php, how can I make it just navigate to 404 error, after I determine there is no something page? Because right now, I just load the my 404 error page into the main div (like all other pages), but it's inconsistent (e.g., it still displays website.com/soemthing in address bar, and there's other problems), and I just want to make it navigate to 404 error. How can I do that?

EDIT: this worked:

echo '<meta http-equiv="refresh" content="0; url = /error" />';

(mywebsite.com/error rewrites to index.php?page=error, which is my 404 error page). Is that a bad approach?

mk12
  • 25,873
  • 32
  • 98
  • 137

1 Answers1

3

Use the header function to send the correct status code:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);

Then you can send your error document with readfile if it’s a static file or with include if it’s a PHP file.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • @Mk12: You need to make sure that there was no output before you call the `header`. Otherwise the HTTP header can not be modified since it’s send together with the first output. You can use the output control to buffer the output (see http://php.net/manual/en/book.outcontrol.php). – Gumbo Oct 25 '09 at 20:28
  • Isn't there some way I can just do navigate_url("/error") ? (website.com/error goes to index.php?page=error, which is what my 404 error page is set to, but I want /error to be displayed in the address bar. – mk12 Oct 25 '09 at 20:29
  • Is there no way to navigate to a url as if the it was a link clicked? – mk12 Oct 25 '09 at 20:29
  • @Mk12: You can’t send a 404 status code and do a HTTP redirect at the same time. The best solution is to retrieve the error document with PHP. – Gumbo Oct 25 '09 at 20:35
  • I don't really care about the 404 status code, just displaying the error page. – mk12 Oct 25 '09 at 20:36
  • @Mk12: Then why are you asking if your approach is bad when you don’t care about? – Gumbo Oct 25 '09 at 20:39
  • .. What does the header thing do? What's bad about not doing it? And couldn't i just do that first then redirect with echoing the meta http-equiv? – mk12 Oct 25 '09 at 20:41
  • Actually, since the http redirect loads index.php (same file) with page=error, I could just check if page=error and then send the 404 status. Is that good? – mk12 Oct 25 '09 at 21:23
  • Yep, it works, I tested at http://web-sniffer.net/ and it said 404 Not Found error.. So now I'm doing it the right way. – mk12 Oct 25 '09 at 21:35
  • @Mk12: It just works because Web-Sniffer is not following the *meta refresh*. – Gumbo Oct 25 '09 at 21:39
  • No, I went directly to website.com/error (where an invalid page redirects), and it gave 404 not found (because I did the header thing in an if statement testing for page = error). – mk12 Oct 25 '09 at 22:47
  • @Gumbo: +1 and thanks for the code perfection using `$_SERVER['SERVER_PROTOCOL']` suggested also in one PHP comment: http://it.php.net/manual/en/function.header.php#92305 that could cause many issues using simply `header("HTTP/1.0 404 Not Found");` – Marco Demaio Nov 20 '10 at 11:29