如何使用htaccess将HTTP_REFERER附加到查询字符串?

时间:2021-10-21 11:04:20

In my .htaccess file I have a set of rules as follows:

在我的.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]

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:

有时,可以通过来自另一个站点(referer)的重定向来访问其中一个规则。我希望能够将引用者附加到查询字符串,如下所示:

RewriteRule ^dir/page2$ /bleh/docs/?id=13&ref=%{HTTP_REFERER} [L,QSA]

However this does not seem to work.

然而,这似乎不起作用。

What am I doing wrong?

我究竟做错了什么?

3 个解决方案

#1


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(由于编辑而不清楚),您正在正确地执行此操作。如果它在那时没有工作,那是因为没有提供引用者。 (这有多种原因;根据浏览者的意愿提供它。)

#2


The variable for the referrer uses the common misspelling: HTTP_REFERER. See this cheat-sheet for some more variable names.

引用者的变量使用常见的拼写错误:HTTP_REFERER。有关更多变量名称,请参阅此备忘单。

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.)

据我所知,重写的URL不会删除原始的Referer标题,因此您仍然可以从代码中获取它而不将其作为查询字符串参数传递(前提是它允许您访问HTTP变量)。 )

#3


A tested method that works for me and transfers the refferer through a 301 redirect. https://webmasters.stackexchange.com/questions/4665/

一种经过测试的方法,适用于我并通过301重定向传输refferer。 https://webmasters.stackexchange.com/questions/4665/

#1


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(由于编辑而不清楚),您正在正确地执行此操作。如果它在那时没有工作,那是因为没有提供引用者。 (这有多种原因;根据浏览者的意愿提供它。)

#2


The variable for the referrer uses the common misspelling: HTTP_REFERER. See this cheat-sheet for some more variable names.

引用者的变量使用常见的拼写错误:HTTP_REFERER。有关更多变量名称,请参阅此备忘单。

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.)

据我所知,重写的URL不会删除原始的Referer标题,因此您仍然可以从代码中获取它而不将其作为查询字符串参数传递(前提是它允许您访问HTTP变量)。 )

#3


A tested method that works for me and transfers the refferer through a 301 redirect. https://webmasters.stackexchange.com/questions/4665/

一种经过测试的方法,适用于我并通过301重定向传输refferer。 https://webmasters.stackexchange.com/questions/4665/