Apache 重写 404:获取请求的 url
在我针对任何 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
%{REQUEST_URI}
尝试一下。我不确定这是否会在 ErrorDocument 中起作用,因为我从未测试过它,但值得尝试。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.没有直接的方法。也不是一个
完美的
。但 PHP 的解决方法很少。例如,我目前使用一个函数来创建每个页面的链接。所以我只需要添加 file_exists() 到主函数(单个函数中的几行)。
这是我用来创建 url 的函数:
这是我创建 url 的常规方法:
我只是想到了这个方法。它很棒,因为即使用户没有单击链接,它也可以让您找到丢失的页面。感谢您让我思考它,现在我将在我的页面中使用它(:
另一个“更简单”的方法(如果您不包装链接)是您将最后访问的几个页面存储在
$_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:
This is my regular way of creating an urls:
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.
看来是没有办法了。
It would seem there isn't a way.