Search code examples
pythongoogle-app-engineseowebapp2no-www

Google App Engine Python Webapp2 301 redirect from www to non-www domain


I have an application built on gae. I use python with webapp2 framework. I need to make 301 redirect from www.my-crazy-domain.com to my-crazy.domain.com so to eliminate www and not-www doubles in search results.

Does anybody have ready-to-use solution? Thanks for any help!


Solution

  • I'v made the trick.

    class BaseController(webapp2.RequestHandler):
       """
       Base controller, all contollers in my cms extends it
       """
    
        def initialize(self, request, response):
            super(BaseController, self).initialize(request, response)
            if request.host_url != config.host_full:
                # get request params without domain
                url = request.url.replace(request.host_url, '')
                return self.redirect(config.host_full+url, permanent=True)
    

    config.host_full contains my primary domain without www. Solution is to check request in base controller and made redirect if domain differs.