【问题标题】:Redirect old website urls in new GAE/Python/webapp2 website在新的 GAE/Python/webapp2 网站中重定向旧网站 url
【发布时间】:2015-04-17 04:17:24
【问题描述】:

为了保持搜索引擎排名,我将 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 的新手,因此我将不胜感激。

谢谢。

【问题讨论】:

    标签: python regex google-app-engine url-redirection webapp2


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 2017-09-25
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多