Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 786
userlond Avatar asked Feb 02 '26 04:02

userlond


1 Answers

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.

like image 168
userlond Avatar answered Feb 04 '26 18:02

userlond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!