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
www.yourdomain.com -- 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
http://yourusername.pythonanywhere.com/something are redirected
http://www.yourdomain.com/something, and so on.
To do this, you need to have a website on the "Web" tab for both domains; the
www.yourdomain.com is your main site, and the one on
is a simple Flask app using Python 3.6, with the following code in the
from flask import Flask, redirect, request app = Flask(__name__) from urllib.parse import urlparse, urlunparse FROM_DOMAIN = "yourusername.pythonanywhere.com" TO_DOMAIN = "www.yourdomain.com" @app.before_request def redirect_to_new_domain(): urlparts = urlparse(request.url) if urlparts.netloc == FROM_DOMAIN: urlparts_list = list(urlparts) urlparts_list = TO_DOMAIN return redirect(urlunparse(urlparts_list), code=301)