Search code examples
pythonregexgoogle-app-enginehttp-redirectwebapp2

Redirect old website urls in new GAE/Python/webapp2 website


To maintain search engine rankings, I am redirecting the URL paths from the old website (static html) to specified paths in the newly designed website. For example: if the old website contains /contact-us.html, the new website should redirect that path to /contact-us

My approach involves creating a handler which simply accepts a path and redirects it:

# handler redirects URL paths from the old website
class oldPathsHandler(BaseHandler):
  def get(self, path):

    # dict contains all paths on the old website and their respective directed URLs
    old_paths = dict()

    # example: index.html should redirect to /home
    old_paths['index.html'] = '/home'
    old_paths['contact-us.html'] = '/contact-us'
    old_paths['php_file.php'] = '/phpfile'
    old_paths['pages.html'] = '/pages'
    old_paths['page-1.html'] = '/page/1'

    # redirects to intended path
    self.redirect(old_paths[path])

# routing
app = webapp2.WSGIApplication([
  ('/', Home),
  ('/(.+)/?', oldPathsHandler),
  ('/get/something', AnotherHandler)
], debug = True)

With this approach, the old links indeed are redirected to the new paths. However, the problem is that other URLs which should not be redirected are also redirected. The path /get/something above will be redirected to /.

The solution to this should exist in the Regular Expression inside the routing configuration. I am a novice at RE, so I would appreciate any assistance.

Thank you.


Solution

  • Change the order of the rules and you should be fine. Later on, when you'll see that nobody is actually visiting the old URLs you could just delete this handler.

    app = webapp2.WSGIApplication([
      ('/', Home),
      ('/get/something', AnotherHandler),
      ('/(.+)/?', oldPathsHandler),
    ], debug = True)