Getting the redirected URL

Many time one needs to get the redirected URL, the reasons might be many but the logic remains the same. HTTP request response is what it's all about, say if an URL has a redirection, one can find the redirected URL in the RAW headers of the response, the location would be the HTTP/1.1 307 Temporary Redirected URL.


REQUEST-RESPONSE

The above images is a reference to the Random images from xkcd as wallpaper script the dynamic URL from xkcd was used to get different redirection responses on every new request.

Getting the redirection location :

Command line:
Using curl

redirect_url=$(curl -w %{redirect_url} $URL)

The variables present in the output format will be substituted by the value for the redirect.
redirect_url => When a HTTP request was made without -L to follow
redirects, this variable will show the actual URL a redirect would take you to. (Added in version 7.18.2)

Python:
Could be done easily with urllib, but that would be a blocking process, so using deferred

from twisted.web.client import getPage
from twisted.web.client import Agent
def command_xkcd(self,rest):
        agent = Agent(reactor)
        d = agent.request(
        'GET',
        URL,
        Headers({'User-Agent': ['Twisted Web Client Example']}),
        None)
        d.addCallback(self.cbRequest)
        return d
         
def cbRequest(self,response):
         return "".join(response.headers.getRawHeaders("location"))

Ruby:
Using 'net/http'

require 'net/http'
def get_redirect(link)
    uri = URI.parse link
    req = Net::HTTP::Get.new(uri.request_uri)
    http = Net::HTTP.new(uri.host)
    res = http.start { |server|
       server.request(req)
    }
    res["location"]
end

Perl:
LWP::UserAgent - Web user agent class

$brw = LWP::UserAgent->new();
$brw->agent("Mozilla/4.0");
$brw->requests_redirectable([ ]);   # Avoid Following redirect
$req = HTTP::Request->new(GET => $URL);
$res = $ua->request($request);
$redirect_url = $response->header('Location');

Got better methods in other languages? Do share it!

Share this