I need the path of the referrer. I don't want the domain name. For example, if the referrer is
http://www.google.com/adsense
I want /adsense
.
Asked
Active
Viewed 8.6k times
88
2 Answers
147
request.referer
returns a string, but you can use Ruby's URI Module to wrap it and then simply ask it for its path:
if URI(request.referer).path == '/adsense'

chadoh
- 4,343
- 6
- 39
- 64
-
4I'm using that URI(request.referer) line and keep getting a URI::InvalidURIError: bad URI(is not URI?) error. any idea what this could be? – kateray Apr 20 '12 at 17:21
-
Hard to say what it could be without more info. Try printing just the `request.referer` into the logs to see why Ruby might not like it. (PS: You may want to ask your question as a separate question, rather than as a comment on the answer to another question. :-) ) – chadoh Apr 26 '12 at 22:19
-
1Check out [this question](http://stackoverflow.com/questions/17148470/ignore-urirequest-referer-path-for-previous-url-when-visiting-directly-to-a-pa) to learn more about `URI::InvalidURIError`. – James Chevalier Dec 27 '13 at 15:25
-
1Oddly enough, the referrer can be an invalid URI even if it isn't blank. I dicovered this with a referrer containing a '|', which raised URI::InvalidURIError I am wrapping the URI.Parse in a begin/rescue/end block. – tomf Nov 12 '15 at 15:17
29
You can access referer with
request.referer

Adrian Serafin
- 7,665
- 5
- 46
- 67
-
3
-
2Thank you for the helpful link, @Fivell. I've added an answer with the information from it to this thread itself so that future visitors aren't plagued with link-rot. – chadoh Nov 15 '11 at 12:56