PHP navigate to error page

梦想与她 提交于 2020-01-06 07:04:41

问题


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:


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.



来源:https://stackoverflow.com/questions/1621972/php-navigate-to-error-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!