views:

201

answers:

1

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?

+1  A: 

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
@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
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
Is there no way to navigate to a url as if the it was a link clicked?
Mk12
Check my question I edited it.
Mk12
@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
I don't really care about the 404 status code, just displaying the error page.
Mk12
@Mk12: Then why are you asking if your approach is bad when you don’t care about?
Gumbo
.. 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
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
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
@Mk12: It just works because Web-Sniffer is not following the *meta refresh*.
Gumbo
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