Search code examples
pythongoogle-app-enginehttp-redirectdnswebapp2

Page for Page Redirect in Google App Engine


I have recently changed domain name for a webapp I run on Google App Engine and I am wondering if there is a simple way to do a page for page redirect from my old website to the new domain. Everything code wise is staying exactly the same but I just want it to go to the new domain.

I am using python and the webapp2 framework for the web application.

I know I could go through and for every single handler do:

webapp2.redirect('the specific url', permanent=True)

But I am hoping for a more elegant solution.


Solution

  • You could just do a global url rewrite. Basically, your webapp is already matching the requested URL and directing it to the appropriate code in the webapp2.WSGIApplication object, so you can simply have all URLs match to the same class, probably with ('/*', myclass) or something like that, and then have that page redirect users to the corresponding page of your new site. Example:

    import webapp2
    newdomain = 'http://www.mynewdomain.com/'
    
    class RedirectPage(webapp2.RequestHandler):
        def get(self, restofurl):
            return webapp2.redirect(newdomain + restofurl, permanent=True)
    
    app = webapp2.WSGIApplication([webapp2.Route(r'/<restofurl:.*>', handler=RedirectPage, name='redirect_page')])
    

    Unfortunately, I can't currently test this to be 100% sure that it works, but I know it would work at least in theory. Ideally, I'd prefer an Apache URL rewrite, but I don't think you have that option.