简体   繁体   English

如何从重定向的URL获取引用者

[英]how to get referrer from a redirected url

I have an url 我有一个网址

domain.com/a

which redirects to 重定向到

domain.com/controller/action/a . domain.com/controller/action/a

How do I get the referrer (ie domain.com/a) in my action for domain.com/controller/action/a ? 如何在我的domain.com/controller/action/a操作中获取引荐来源(即domain.com/a)?

One option was to add the referring domain as a parameter . 一种选择是将引用域添加为参数。

domain.com/controller/action/a?referral=domain.com/a . domain.com/controller/action/a?referral=domain.com/a

Is there a way to get the referrer without passing old referrer as a parameter. 有没有办法获得引用者而不传递旧的引用者作为参数。 Like we would get from **request.referrer** . 就像我们从**request.referrer** request.referrer doesn't seem to work with redirected urls. request.referrer似乎不适用于重定向的URL。

I am using Ruby on Rails for my development. 我正在使用Ruby on Rails进行开发。

I store the referrer path in the session right before redirection 我在重定向之前将referrer路径存储在会话中

session[:referrer]=url_for(params)

and then use it where I need it via session[:referrer] . 然后通过session[:referrer]在我需要的地方使用它。

我相信您正在寻找request.referrer属性: http//rack.rubyforge.org/doc/classes/Rack/Request.html#M000280

We use this line in our application_controller.rb to save the http_referer when a visitor visits our site: 我们在application_controller.rb中使用此行来保存访问者访问我们网站时的http_referer:

session[:http_referer] = request.env["HTTP_REFERER"]

Later on, we will save the value into our model for tracking. 稍后,我们会将值保存到我们的模型中进行跟踪。

May be late, but just to add on. 可能会迟到,但只是加上。 I think it is important to remove the session after using it. 我认为使用它后删除会话很重要。

redirect_to session.delete(:referrer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM