Google App Engine redirection from naked domain to www

When project is created on Google Cloud Platform, 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 "" and they are taken to url Some type and they are taken to Often is the main url for website.

Redirection is needed for case when users type to be taken to 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 with examples for Webapp2 and Flask.


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('', [Route(r'<:.*>', handler=RedirectHandler)]),  
    # other routes
], debug=True)


In routes we set DomainRoute for 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 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

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