PHP中的404错误后重定向到首页 [英] Redirecting to frontpage after 404 error in PHP
问题描述
我有一个php网页,现在使用自定义错误页面没有找到一个页面。自定义错误页面包含在PHP中。
I have a php web page that now uses custom error pages when a page is not found. The custom error pages are included in PHP.
所以当有人键入不存在的URL时,我只包含一个错误页面,错误页面以: / p>
So when somebody types in an URL that does not exists I just include an error page, and the error page starts with:
<?php header("HTTP/1.1 404 Not> Found"); ?>
这也告诉抓取工具该页面不存在。
This also tells crawlers that the page does not exist.
现在我已经建立了一个新的系统。当用户输入错误的网址时,用户将被发送回首页,并在首页上显示一条消息。我重定向到这样的首页:
Now I have set up a new system. When a user types a wrong url, the user is sent back to the frontpage and a message is displayed on the frontpage. I redirect to the frontpage like this:
header('Location:' . __TINY_URL . '/');
现在的问题是PHP只是发回一个200代码,找到页面。
Now the problem is PHP just sends back a 200 code, page found.
如何混合这两个在首页上创建404代码。
How can I mix these two to create a 404 code on the frontpage.
这是一个很好的呈现和错误的方法
And is this overall a nice way of presenting and error page.
推荐答案
错误后重定向不是一个好主意。对于喜欢输入/编辑网址的用户而言,这是非常烦人的,因为如果您输入错字,您将被重定向到任意页面,必须重新开始。
Redirecting after an error is not a very good idea. It's especially annoying for people who like to type in/edit URLs, because if you make a typo, you'll get redirected to some arbitrary page and have to start over.
我建议你根本不这样做。如果你想,你可以让你的错误页面看起来像你的头版,虽然我认为这有点令人困惑。
I suggest you don't do this at all. If you want to, you can have your error page look like your front page though, albeit I think that'd be somewhat confusing.
这篇关于PHP中的404错误后重定向到首页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!