0

This was marked as a possible duplicate, but I'm not looking for the full path of the url of the current page. Instead, I'm looking for the full path of the referer url.

~~~

I am trying to get the full path of a referer using Ruby. Currently when I do:

request.referer

I only get the domain (example: https://stackoverflow.com/). Instead, I want the full URL with the path.

Any advice on how to do this?

sharataka
  • 5,014
  • 20
  • 65
  • 125

1 Answers1

3

To get full path of request URI use REQUEST_URI

request.env['REQUEST_URI']

To get full path of the referer use HTTP_REFERER

request.env['HTTP_REFERER']

The complete list of available options in request.env hash are

"GATEWAY_INTERFACE"=>"CGI/1.1", 
"PATH_INFO"=>"/accounts/1/users/new", 
"QUERY_STRING"=>"", 
"REMOTE_ADDR"=>"127.0.0.1", 
"REMOTE_HOST"=>"abcd.com", 
"REQUEST_METHOD"=>"GET", 
"REQUEST_URI"=>"http://localhost:3000/accounts/1/users/new", 
"SCRIPT_NAME"=>"", 
"SERVER_NAME"=>"localhost", 
"SERVER_PORT"=>"3000", 
"SERVER_PROTOCOL"=>"HTTP/1.1", 
"SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/1.9.3/2014-11-13)", 
"HTTP_HOST"=>"localhost:3000", 
"HTTP_CONNECTION"=>"keep-alive", 
"HTTP_UPGRADE_INSECURE_REQUESTS"=>"1", 
"HTTP_USER_AGENT"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36", 
"HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", 
"HTTP_REFERER"=>"http://localhost:3000/accounts/1/users", 
"HTTP_ACCEPT_ENCODING"=>"gzip, deflate, br", 
"HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.9,hi;q=0.8", 
"HTTP_VERSION"=>"HTTP/1.1", 
"REQUEST_PATH"=>"/accounts/1/users/new", 
"ORIGINAL_FULLPATH"=>"/accounts/1/users/new"

You can concatenate string to get the desired result from above available values in env hash.

Satishakumar Awati
  • 3,604
  • 1
  • 29
  • 50