为了保持搜索引擎排名,我将旧网站(静态html)的URL路径重定向到新设计的网站中的指定路径。例如:如果旧网站包含/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)