Apache 重写 40​​4:获取请求的 url

发布于 2024-11-09 09:20:47 字数 155 浏览 0 评论 0原文

在我针对任何 404 重定向到的错误页面上,我想记录用户尝试访问的 URL。

我已经尝试过,但不起作用:

ErrorDocument 404 /error/?url=%{HTTP_REFERRER}

谁能告诉我该怎么做?

On my error page that I redirect to for any 404s, I'd like to record the url that the user tried to get to.

I've tried this but it doesn't work:

ErrorDocument 404 /error/?url=%{HTTP_REFERRER}

Can anyone tell me how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

流心雨 2024-11-16 09:20:47

使用 %{REQUEST_URI} 尝试一下。我不确定这是否会在 ErrorDocument 中起作用,因为我从未测试过它,但值得尝试。

ErrorDocument 404 /error/?url=%{REQUEST_URI}

Try it with %{REQUEST_URI}. I'm not certain this will work in ErrorDocument since I've never tested it, but it's worth trying.

ErrorDocument 404 /error/?url=%{REQUEST_URI}
止于盛夏 2024-11-16 09:20:47

没有直接的方法。也不是一个完美的。但 PHP 的解决方法很少。

例如,我目前使用一个函数来创建每个页面的链接。所以我只需要添加 file_exists() 到主函数(单个函数中的几行)。

这是我用来创建 url 的函数:

function url ($Value)
  {
  // Do some stuff with the url
  // [Not showed]

  if (!file_exists("/internal/path/".$Value))
    {
    // Call a function to store the error in a database
    error ("404", $Value);

    // One way of handling it. Replace '/' for ' ' and search that string.
    // Example: "path/to/page" would become "path to page".
    $Value=str_replace("/","%20",$Value); 
    return "http://www.example.com/search=".$Value;
    }
  else
    {
    // If the page exists, create the normal link.
    return $FullUrl;
    }
  }

这是我创建 url 的常规方法:

<?php url('path/to/page'); ?>

我只是想到了这个方法。它很棒,因为即使用户没有单击链接,它也可以让您找到丢失的页面。感谢您让我思考它,现在我将在我的页面中使用它(:

另一个“更简单”的方法(如果您不包装链接)是您将最后访问的几个页面存储在 $_SESSION[ 'lastpage'];$_SESSION['lastlastpage'];,如果发现 404,则存储用户尝试访问损坏页面的相应页面。这不是一个完美的页面。解决方案自您必须手动找到上一页中的损坏链接,但至少它可以让您知道它在哪里。

缺点:如您所见,这两种解决方案仅适用于内部损坏的链接。

There isn't a direct way. Nor a perfect one. But there are few workarounds with PHP.

For example, I currently use a function to create the links of each page. So I would just need to add file_exists() to the main function (few lines in a single function).

This is the function I would use to create urls:

function url ($Value)
  {
  // Do some stuff with the url
  // [Not showed]

  if (!file_exists("/internal/path/".$Value))
    {
    // Call a function to store the error in a database
    error ("404", $Value);

    // One way of handling it. Replace '/' for ' ' and search that string.
    // Example: "path/to/page" would become "path to page".
    $Value=str_replace("/","%20",$Value); 
    return "http://www.example.com/search=".$Value;
    }
  else
    {
    // If the page exists, create the normal link.
    return $FullUrl;
    }
  }

This is my regular way of creating an urls:

<?php url('path/to/page'); ?>

I just thought about this method. It's great as it allows you to find missing pages even IF the user doesn't click on the links. Thank you for making me think about it and now I'll use it in my page (:

Another 'simpler' method (in case you do not wrap links) is that you store last couple of pages visited in $_SESSION['lastpage']; and $_SESSION['lastlastpage'];, if 404 is found then store the corresponding page from which the user tried to access the broken page. It's not a perfect solution since you have to manually find the broken link in the previous page, but at least it gives you some idea of where it is.

Disadvantage: As you can see, both solutions ONLY work with internal broken links.

一指流沙 2024-11-16 09:20:47

看来是没有办法了。

It would seem there isn't a way.

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