当数据库中缺少项目时,PHP 或 htaccess 使动态 url 页面转到 404

发布于 2024-10-03 03:33:55 字数 717 浏览 10 评论 0原文

典型场景:

  1. 数据库项目显示在页面http://...?item_id=467
  2. 用户有一天删除 项目
  3. Google 或用户的 尝试访问 http://...?item_id=467
  4. PHP 深入数据库并发现项目不再存在,所以现在 PHP 必须告诉 Google/用户通过 404 标头和页面发现该项目不存在。

根据这个答案我明白没有办法重定向到404通过 PHP 的 Apache 页面,除非在代码中发送 404 标头 + 读取默认 404 页面的所有内容并将其发送到客户端。

问题:我已经有一个 Apache parsed 自定义 404.shtml 页面,所以显然我只想使用该页面。 但是如果我通过 PHP 读取 shtml 页面,Apache 将不再解析它

那么你建议我做什么?

我也可以使用一些技巧来玩 htaccess 吗?

谢谢,

Typical scenario:

  1. DB items are displaied in page http://...?item_id=467
  2. User one day deletes
    the item
  3. Google or a user
    attempts to access http://...?item_id=467
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-10-10 03:33:55

唔。我想到了两个想法:

  • 使用 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 straw

  • Fetch 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.

波浪屿的海角声 2024-10-10 03:33:55

如果您使用的是 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.

十级心震 2024-10-10 03:33:55

昨天我正试图做同样的事情。

Pekka 的 file_get_contents/include 是否会导致发送 404 状态标头?也许您需要在包含自定义错误页面之前执行此操作?

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

您可以使用此 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?

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

You can test using this Firefox extension.

秋风の叶未落 2024-10-10 03:33:55

我一直在寻找像您需要的东西,所以您有一个页面:

http://example.com/page?item_id=456

如果稍后您想要,如果项目丢失,您将被重定向到:

http://example.com/page_not_found?item_id=456

实际上,我发现仅使用原始页面作为 404 页面是更易于维护的解决方案。

<?php
    $item = findItem( $_GET['item_id']);
    if($item === false){
        //show 404 page sending correct header and then include 404 message
        header( $_ENV['SERVER_PROTOCOL'].' 404 Not Found', true );
        // you can still use $_GET['item_id'] to customize error message
        // "maybe you were looking for XXX item"
        include('somepath/missingpage.php');
        return;
    }

    //continue as usual with normal page

?>

因此,如果项目不再位于数据库中,则会显示 404 页面,但您可以在替换或错误消息中提供自定义项目。

I was looking exactly for something like you needed, so you have a page:

http://example.com/page?item_id=456

and if later you want that if item is missing you are redirected to:

http://example.com/page_not_found?item_id=456

In reality I found it is much more maintainable solution to just use the original page as 404 page.

<?php
    $item = findItem( $_GET['item_id']);
    if($item === false){
        //show 404 page sending correct header and then include 404 message
        header( $_ENV['SERVER_PROTOCOL'].' 404 Not Found', true );
        // you can still use $_GET['item_id'] to customize error message
        // "maybe you were looking for XXX item"
        include('somepath/missingpage.php');
        return;
    }

    //continue as usual with normal 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文