how to get referrer from a redirected url
Asked Answered
N

4

14

I have an url

domain.com/a

which redirects to

domain.com/controller/action/a .

How do I get the referrer (i.e domain.com/a) in my action for domain.com/controller/action/a ?

One option was to add the referring domain as a parameter .

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 doesn't seem to work with redirected urls.

I am using Ruby on Rails for my development.

Neale answered 17/1, 2012 at 17:11 Comment(1)
@okliv actually I forgot which one I used for my code :) .Neale
W
11

I store the referrer path in the session right before redirection

session[:referrer]=url_for(params)

and then use it where I need it via session[:referrer].

Womanhater answered 10/4, 2012 at 3:3 Comment(1)
I guess there isn't other waysHibernicism
P
9

I believe you are looking for the request.referrer property: http://rack.rubyforge.org/doc/classes/Rack/Request.html#M000280

Poem answered 17/1, 2012 at 17:25 Comment(0)
K
4

We use this line in our application_controller.rb to save the http_referer when a visitor visits our site:

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

Later on, we will save the value into our model for tracking.

Kassab answered 11/4, 2012 at 3:9 Comment(0)
C
0

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)
Coxswain answered 14/9, 2015 at 2:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.