userlond userlond - 1 year ago 107
Python Question

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 to so to eliminate www and not-www doubles in search results.

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download