Python has many great modules to deal with HTTP connections. A simple one to look at to day is the requests module. We’ll look at how to query the HTTP request for the HEAD information and get the Location to resolve a URL to its full location.

Why is this needed or helpful? Many times we are given a short URL and our program requires the full URL. In a recent downloading script the function would use the last URL part as the file name. This works well to download the file but wouldn’t name the file correctly.

For example the SSMS download from Microsoft is currently The download script would use “ssmsfullsetup” as the filename for the download. This file has no extension and makes it harder to parse the script or adds more unneeded steps. So finding the final Location helps with this.

First we will handle all the steps with the requests module so lets begin our import and setting up our function.

import requests

def resolver(loc, verbose=False, hops=1):
        print(f"{hops}: {loc}") 
    # get the head request
    url = requests.head(loc)
    # get the Location from the HEAD
    url = url.headers\["Location"\]
    # compare current URL to the next URL if different proceed with the next location
    # then return the final URL
        nexturl = resolver(url, verbose, hops)
        if nexturl == url:
            return nexturl
        return url

We need this function to be recursive so the URL keeps searching for the final destination. In the case of the SSMS download we can see when running Verbosely we go through a few redirects:


Hop 3 is the final destination and is returned for final use. Now in my download script we can use this final URL since the filename will end up being SSMS-Setup-ENU.exe.

For our website we have a .COM domain since the .NETWORK domain can be hard to find and the Location in the HEAD can help forward the user to our website.


Since this is a simple function we can just call the function and print the results. If we want to see the redirects we can with the verbose option.

print(resolver("", verbose=True))

Lets try this short url,, and see where the redirects are going:

print(resolver("", verbose=True))

Source code is on our GitHub repo