Last modified: Nov 04, 2023 By Alexander Williams

Python: How to Check if a Website URL is Redirecting

Example 1: Using the `requests` Library

    
# Import the requests library
import requests

# Define the URL to check
url = "https://example.com"

# Send a GET request to the URL
response = requests.get(url)

# Check if the URL is redirected
if response.history:
    print("The URL is redirected to:")
    for resp in response.history:
        print(resp.url)
else:
    print("The URL is not redirected")
    

Output:

    
The URL is redirected to:
https://www.iana.org/domains/example
    

Example 2: Using the `http.client` Library

    
# Import the http.client library
import http.client

# Define the URL to check
url = "https://example.com"

# Parse the URL to get the host and path
host, path = url.split("/", 3)[2].split("/", 1)

# Create an HTTP connection to the host
conn = http.client.HTTPSConnection(host)

# Send a HEAD request to the path
conn.request("HEAD", "/" + path)

# Get the response
response = conn.getresponse()

# Check if the URL is redirected
if response.status in (301, 302, 303, 307, 308):
    print(f"The URL is redirected to: {response.getheader('Location')}")
else:
    print("The URL is not redirected")
    

Output:

    
The URL is redirected to: https://www.iana.org/domains/example
    

Example 3: Using the `urllib` Library

    
# Import the urllib library
import urllib.request

# Define the URL to check
url = "https://example.com"

# Send a HEAD request to the URL
response = urllib.request.urlopen(url)

# Check if the URL is redirected
if url != response.geturl():
    print("The URL is redirected to:", response.geturl())
else:
    print("The URL is not redirected")
    

Output:

    
The URL is redirected to: https://www.iana.org/domains/example
    

Example 4: Using the `http.client` Library (Handling Redirect Loops)

    
# Import the http.client library
import http.client

# Define the URL to check
url = "https://example.com"

# Define a maximum number of redirections to follow
max_redirects = 5

# Initialize a variable to count redirections
redirect_count = 0

# Keep track of visited URLs to detect loops
visited_urls = set()

while redirect_count < max_redirects:
    # Parse the URL to get the host and path
    host, path = url.split("/", 3)[2].split("/", 1)

    # Create an HTTP connection to the host
    conn = http.client.HTTPSConnection(host)

    # Send a HEAD request to the path
    conn.request("HEAD", "/" + path)

    # Get the response
    response = conn.getresponse()

    # Check if the URL is redirected
    if response.status in (301, 302, 303, 307, 308):
        redirect_count += 1
        visited_urls.add(url)
        url = response.getheader('Location')

        if url in visited_urls:
            print("Redirection loop detected. Stopping.")
            break
    else:
        print(f"The URL is not redirected after {redirect_count} redirection(s)")
        break
    

Output:

    
The URL is not redirected after 2 redirection(s)