Google App Engine redirection from naked domain to www

When project is created on Google Cloud Platform, my-project-name.appspot.com domain is automatically created and deployed Google App Engine (GEA) application handles request there, but if web application is production ready, we want that it handles requests for our own (custom, more name friendly) domain.

Further we want that website has only one unique domain where visitors will go. People often type in browser "mydomain.com" and they are taken to url http://mydomain.com. Some type www.mydomain.com and they are taken to http://www.mydomain.com. Often www.mydomain.com is the main url for website.

Redirection is needed for case when users type mydomain.com to be taken to www.mydomain.com. This can be done in several ways, we can make redirection in Nginx / Apache (or what ever server) but for Google App Engine situation is different. 

We need to set custom domain in Google App Engine settings, verify that domain is ours, update DNS records for our custom domain, but redirection needs to be handled in application itself.

Since I was dealing myself with this issue lately I set repository on Github https://github.com/zdenulo/gae_redirection with examples for Webapp2 and Flask.

Webapp2

Webapp2 is framework included in GAE Python SDK so it's often natural choice to develop webapplications in Python. Webapp2 includes extracts where DomainRoute class is which is designed for case as this. Here is documentation for DomainRoute. Basically we need to define in routing rules domain and then routes to here requests will be redirected:

class RedirectHandler(webapp2.RequestHandler):
    """Redirects from naked to www subdomain domain"""

    def get(self, path):
        url = self.request.url
        protocol, sub_url = url.split('://', 1)
        new_url = "{}://www.{}".format(protocol, sub_url)
        return self.redirect(new_url, permanent=True)

app = webapp2.WSGIApplication([
    DomainRoute('mydomain.com', [Route(r'<:.*>', handler=RedirectHandler)]),  
    # other routes
], debug=True)

 

In routes we set DomainRoute for mydomain.com and RedirectHandler which will handle all requests (based on regex rule).

In RedirectHandler itself, url is parsed and www string is inserted to url and then redirected with permanent equals True which means that it http code for redirect will be 302 (permanent redirect). It's possible to adjust this based on needs, but it's fancy way how to handle redirection

Flask

Flask is the most popular Python's micro framework and redirect is done a bit different. For flask we can override function "before_request" which as name says will run before every request. Code snippet is inspired from http://stackoverflow.com/questions/9766134/how-do-i-redirect-to-the-www-version-of-my-flask-site-on-heroku/10964868#10964868

@app.before_request
def redirect_nonwww():
    """Redirect requests from naked to www subdomain."""
    url = request.url
    urlparts = urlparse(url)
    if urlparts.netloc == DOMAIN_NAME:
        urlparts_list = list(urlparts)
        urlparts_list[1] = 'www.' + DOMAIN_NAME
        new_url = urlunparse(urlparts_list)
        logging.debug("redirecting from {} to {}".format(url, new_url))
        return redirect(new_url, code=301)

 I'm not sure if these examples work for all possible cases, but it's good starting point to play with.

 

 

 

blog comments powered by Disqus