PHP导航到错误页面 [英] PHP navigate to error page
问题描述
在我的网站中,website.com/downloads实际上转到website.com/index.php?page=downloads(使用Apache重写).我有一个自定义404找不到错误页面集.如果我执行website.com/dkfjf/fdf,将转至404错误,但是如果我执行website.com/something,则将转至index.php?page = something,因此在我的index.php中,我该怎么办?确定我没有 something
页面后,使其仅导航至404错误?因为现在,我只是将404错误页面加载到主div中(就像所有其他页面一样),但是不一致(例如,它仍然在地址栏中显示website.com/soemthing,并且还有其他问题),我只是想要使其导航到404错误.我该怎么办?
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?
这可行:
echo '<meta http-equiv="refresh" content="0; url = /error" />';
(mywebsite.com/error重写为index.php?page = error,这是我的404错误页面).这是一个不好的方法吗?
(mywebsite.com/error rewrites to index.php?page=error, which is my 404 error page). Is that a bad approach?
推荐答案
使用 header
函数发送正确的状态码:
Use the header
function to send the correct status code:
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
然后,如果它是静态文件,则可以使用 readfile
发送错误文档或使用 include
(如果它是PHP文件).
Then you can send your error document with readfile
if it’s a static file or with include
if it’s a PHP file.
这篇关于PHP导航到错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!