If http://foo.com
redirects to 1.2.3.4
which then redirects to http://finalurl.com
, how can I use Ruby to find out the landing URL "http://finalurl.com"?
If http://foo.com
redirects to 1.2.3.4
which then redirects to http://finalurl.com
, how can I use Ruby to find out the landing URL "http://finalurl.com"?
Here's two ways, using both HTTPClient and Open-URI:
require 'httpclient'
require 'open-uri'
URL = 'http://www.example.org'
httpc = HTTPClient.new
resp = httpc.get(URL)
puts resp.header['Location']
>> http://www.iana.org/domains/example/
open(URL) do |resp|
puts resp.base_uri.to_s
end
>> http://www.iana.org/domains/example/