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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

@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 php.net/manual/en/book.outcontrol.php).
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.
Is there no way to navigate to a url as if the it was a link clicked?
@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.
I don't really care about the 404 status code, just displaying the error page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.