How do I create a web app that redirects from one domain to another?

Sometimes you need to write a website that simply redirects from one domain to another. For example, you might have created a website at, and it's become popular enough that you decided to upgrade to a paid plan and host it on -- but there are lots of links out there that go to the old domain, and you want them to continue working.

The best solution is to have a simple website running at which, when it receives a request, redirects the user to the new domain -- so requests to are redirected automatically to, and so on.

To do this, you need to have a website on the "Web" tab for both domains; the one on is your main site, and the one on is a simple Flask app using Python 3.6, with the following code in the file:

from flask import Flask, redirect, request

app = Flask(__name__)

from urllib.parse import urlparse, urlunparse


def redirect_to_new_domain():
    urlparts = urlparse(request.url)
    if urlparts.netloc == FROM_DOMAIN:
        urlparts_list = list(urlparts)
        urlparts_list[1] = TO_DOMAIN
        return redirect(urlunparse(urlparts_list), code=301)