---
title: "Getting the Redirected URL"
description: "Many time one needs to get the redirected URL, the reasons might be many but the logic remains the same. HTTP request in JS."
url: "https://h3manth.com/scribe/getting-redirected-url/"
canonical_url: "https://h3manth.com/scribe/getting-redirected-url/"
markdown_url: "https://h3manth.com/scribe/blog/getting-redirected-url.md"
publishDate: "2010-11-12T00:00:00.000Z"
author: "Hemanth HM"
category: "linux"
tags: ["bash","web"]
word_count: 262
reading_time_minutes: 2
x_markdown_tokens: 676
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# 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 in JS.

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.

![](http://www.h3manth.com/sites/files/eKtJF.png)

REQUEST-RESPONSE 

The above images is a reference to the [ Random images from xkcd as wallpaper script](http://www.h3manth.com/content/random-images-xkcd-wallpaper) 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!**
