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/
Another way, using Curb:
def get_redirected_url(your_url)
result = Curl::Easy.perform(your_url) do |curl|
curl.follow_location = true
end
result.last_effective_url
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With