Reputation: 11
Does anyone know whether there is a simple way to record all urls on a path of url redirections?
For example: the url: (url 1) redirects to (url 2) which redirects to (url 3).
I would like to write a script that takes an input the string (url 1) and returns (url 2) and (url 3).
Is there a simple way to do this (ideally without using javascript)?
Upvotes: 1
Views: 123
Reputation: 3388
require 'net/http'
def redirect_tracker(url)
paths_array = [url]
code = nil
begin
response = Net::HTTP.start(URI.parse(url).host){|http| http.request Net::HTTP::Head.new(url) }
code = response.code.to_i
paths_array << url if url = response['location']
end while (301..303).include?(code)
return paths_array
end
redirect_tracker('http://google.com')
# => ["http://google.com/", "http://www.google.com/"]
Upvotes: 0
Reputation: 1558
in python:
import requests
url = 'http://google.com'
r = requests.get(url)
urls = [e.url for e in r.history]+[r.url]
then you get:
>>>urls
[u'http://google.com', u'http://www.google.com/']
it works only if the redirection are in the http layer (the 30x range)
Upvotes: 1
Reputation: 385917
Given a $response
from LWP,
my @request_uris;
while ($response) {
unshift @request_uris, $response->request->uri;
$response = $response->previous;
}
Upvotes: 0