Forums

redirection from mydomain.com to www.mydomain.com

following this guide: https://help.pythonanywhere.com/pages/RedirectWebApp

I like the idea to have 2 webapps, first one at mydomain.com and second one at www.mydomain.com

for this I trying to edit the file:

/var/www/www_redv6_com_wsgi.py

wich have this code now:

import sys
import flask

# add your project directory to the sys.path
project_home = '/home/abelardodiaz/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
#from main import app as application  # noqa
from main import app as application

and I need to add something like this:

from flask import Flask, redirect, request

app = Flask(__name__)

from urllib.parse import urlparse, urlunparse

FROM_DOMAIN = "redv6.com"
TO_DOMAIN = "www.redv6.com"

@app.before_request
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)

DId I miss something? it should work?

I see only one web app deployed on your account -- do you have another account where you deployed the second web app? For this solution to work, you need to have to separate web apps deployed on PythonAnywhere.