当数据库中缺少项目时,PHP 或 htaccess 使动态 url 页面转到 404
典型场景:
- 数据库项目显示在页面
http://...?item_id=467
- 用户有一天删除 项目
- Google 或用户的 尝试访问
http://...?item_id=467
- PHP 深入数据库并发现项目不再存在,所以现在 PHP 必须告诉 Google/用户通过 404 标头和页面发现该项目不存在。
根据这个答案我明白没有办法重定向到404通过 PHP 的 Apache 页面,除非在代码中发送 404 标头 + 读取默认 404 页面的所有内容并将其发送到客户端。
问题:我已经有一个 Apache parsed 自定义 404.shtml 页面,所以显然我只想使用该页面。 但是如果我通过 PHP 读取 shtml 页面,Apache 将不再解析它。
那么你建议我做什么?
我也可以使用一些技巧来玩 htaccess 吗?
谢谢,
Typical scenario:
- DB items are displaied in page
http://...?item_id=467
- User one day deletes
the item - Google or a user
attempts to accesshttp://...?item_id=467
- PHP diggs into DB and sees items does not exist anymore, so now PHP must tell
Google/user that item is not existing via a 404 header and page.
According to this answer I undertstood there is no way to redirect to 404 Apache page via PHP unless sending in code the 404 header + reading and sending down to client all the contents of your default 404 page.
The probelm: I already have an Apache parsed custom 404.shtml page, so obvioulsy I would like to simply use that page.
But if i read an shtml page via PHP it won't be parsed by Apache anymore.
So what do you suggest me to do?
Is there maybe some trick I could use palying with htaccess too?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
唔。我想到了两个想法:
使用
header("Location:...") 重定向到 404 页面
- 但这不是符合标准的行为。我只会将其用作最后一根稻草使用
file_get_contents("http://mydomain.com/404.shtml"); 获取并输出 Apache 解析的 SHTML 文件;
- 也不是 < em>确实是最佳的,因为请求是向网络服务器发出的,但我认为在大多数情况下是可以接受的。我怀疑您可以在
.htaccess
中执行任何操作,因为 PHP 脚本在解析所有重写规则后运行。Hmm. Two ideas come to mind:
Redirect to the 404 page using
header("Location:...")
- this is not standards-compliant behaviour though. I would use that only as a last strawFetch and output the Apache-parsed SHTML file using
file_get_contents("http://mydomain.com/404.shtml");
- also not really optimal because a request is made to the web server but, I think, acceptable in most cases.I doubt there is anything you can do in
.htaccess
because the PHP script runs after any rewrite rules have already been parsed.如果您使用的是 apache mod_php,请使用
virtual('/404.shtml');
向您的用户显示解析后的 shtml 页面。IF you are using apache mod_php, use
virtual('/404.shtml');
to display the parsed shtml page to your user.昨天我正试图做同样的事情。
Pekka 的 file_get_contents/include 是否会导致发送 404 状态标头?也许您需要在包含自定义错误页面之前执行此操作?
您可以使用此 Firefox 扩展进行测试。
I was trying to do this exact same thing yesterday.
Does Pekka's file_get_contents/include result in a 404 status header being sent? Perhaps you need to do this before including the custom error page?
You can test using this Firefox extension.
我一直在寻找像您需要的东西,所以您有一个页面:
如果稍后您想要,如果项目丢失,您将被重定向到:
实际上,我发现仅使用原始页面作为 404 页面是更易于维护的解决方案。
因此,如果项目不再位于数据库中,则会显示 404 页面,但您可以在替换或错误消息中提供自定义项目。
I was looking exactly for something like you needed, so you have a page:
and if later you want that if item is missing you are redirected to:
In reality I found it is much more maintainable solution to just use the original page as 404 page.
So if item is no longer in the DB, the 404 page is showed but you can provide custom items in replace or error messages.