1from flask import Flask, redirect, request
2
3app = Flask(__name__)
4
5from urllib.parse import urlparse, urlunparse
6
7FROM_DOMAIN = "yourusername.pythonanywhere.com"
8TO_DOMAIN = "www.yourdomain.com"
9
10@app.before_request
11def redirect_to_new_domain():
12 urlparts = urlparse(request.url)
13 if urlparts.netloc == FROM_DOMAIN:
14 urlparts_list = list(urlparts)
15 urlparts_list[1] = TO_DOMAIN
16 return redirect(urlunparse(urlparts_list), code=301)
17