如何使用 htaccess 将 HTTP_REFERER 附加到查询字符串?
在我的 .htaccess 文件中,我有一组规则,如下所示:
RewriteRule ^dir/page1$ /bleh/docs/?id=12 [L,QSA]
RewriteRule ^dir/page2$ /bleh/docs/?id=13 [L,QSA]
RewriteRule ^dir/page3$ /bleh/docs/?id=14 [L,QSA]
有时可以通过从另一个站点(引用者)重定向来访问其中一个规则。 我希望能够将引用者附加到查询字符串,如下所示:
RewriteRule ^dir/page2$ /bleh/docs/?id=13&ref=%{HTTP_REFERER} [L,QSA]
但这似乎不起作用。
我究竟做错了什么?
In my .htaccess file I have a set of rules as follows:
RewriteRule ^dir/page1$ /bleh/docs/?id=12 [L,QSA]
RewriteRule ^dir/page2$ /bleh/docs/?id=13 [L,QSA]
RewriteRule ^dir/page3$ /bleh/docs/?id=14 [L,QSA]
Sometimes one of these rules may be accessed via a redirect from another site (referer). I would like to be able to append the referrer to the query string like this:
RewriteRule ^dir/page2$ /bleh/docs/?id=13&ref=%{HTTP_REFERER} [L,QSA]
However this does not seem to work.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将其拼写为 HTTP_REFERER(由于编辑原因不清楚),那么您的做法是正确的。 如果此时不起作用,那是因为未提供引用者。 (有很多原因;提供它由浏览器自行决定。)
If you're spelling it HTTP_REFERER (this is unclear due to edits), you're doing it correctly. If it isn't working at that point, it's because the referer isn't being supplied. (Which there are any number of reasons for; supplying it is at the browser's discretion.)
引荐来源网址的变量使用常见的拼写错误:
HTTP_REFERER
。 有关更多变量名称,请参阅此备忘单。据我所知,重写的 URL 不会删除原始的
Referer
标头,因此您仍然应该能够从代码中获取它,而无需将其作为查询字符串参数传递(前提是它给出您可以访问 HTTP 变量。)The variable for the referrer uses the common misspelling:
HTTP_REFERER
. See this cheat-sheet for some more variable names.As far as I know, a rewritten URL won't remove the original
Referer
header though, so you should still be able to fetch it from your code without passing it as a query string parameter (provided it gives you access to the HTTP variables.)一种经过测试的方法,对我有用,并通过 301 重定向传输引用者。
https://webmasters.stackexchange.com/questions/4665/
A tested method that works for me and transfers the refferer through a 301 redirect.
https://webmasters.stackexchange.com/questions/4665/