在新 GAE/Python/webapp2 网站中重定向旧网站网址

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

为了保持搜索引擎排名,我将 URL 路径从旧网站(静态 html)重定向到新设计网站中的指定路径。例如:如果旧网站包含 /contact-us.html,新网站应该将该路径重定向到 /contact-us

我的方法涉及创建一个处理程序,它只接受一个路径并将其重定向:

# 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)

使用这种方法,旧链接确实被重定向到新路径。但是,问题是其他不应该被重定向的 URL 也被重定向了。上面的路径 /get/something 将被重定向到 /

这个问题的解决方案应该存在于路由配置中的正则表达式中。我是 RE 的新手,所以我将不胜感激任何帮助。

谢谢。

改变规则的顺序应该没问题。稍后,当您发现实际上没有人访问旧 URL 时,您可以删除此处理程序。

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