Python - Redirector

Page content

Redirector App

wrote a little redirector app and tought i will explain and share it here. it’s a bit like a url shortener, but you can define the “shortcut” of the URL.

how does it work

it basically consists of a Text File wir Redirection URL’s.



Call it

so, when you open a Browser and Request the URL:, you get redirected to

from flask import Flask, redirect, request
import datetime
import os
import random

# Vars

app = Flask(__name__)

# Load redirection data from a text file
def get_redirections():
    redirections = {}
    with open(redirect_file,"r") as file:
        for line in file:
            path, url = line.strip().split(',')
            redirections[path] = url
    return redirections

# Main
def index():
    return 'Hello, World!'

# Redirect to Random URL
def random_path():
    redirections = get_redirections()

    # Get Random Path
    random_url = random.choice(list(redirections.values()))
    return redirect(random_url)

# Redirect
def redirect_path(path):

    # File Changed ?
    redirections = get_redirections()

    # Check if the path exists in the redirections dictionary
    if path in redirections:

        url = redirections[path]
        return redirect(url)

    # If the path does not exist, return a 404 Not Found error
    return 'Not Found', 404

if __name__ == '__main__':

get it running

you need the ususal Stuff

  • Linux (of BSD Machine here)
  • Public IP with FQDN
  • TLS Cert
  • Nginx Server

I’m not gonna describe how todo it … there are plenty of examples around.

one thing to mention. the nginx acts as a reverse proxy. get all requests and pass them to the application like this:

    location / {
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_redirect off;
      proxy_buffering off;

run with poetry

poetry run flask --app main run --port=5555

and open your Browser and you should be redirected to the appropriate Domain.

Any Comments ?

sha256: b55fa755f8ab19ed7503e946a877087852f6e95d00bfa1f0ac3e984746e33bb0