【发布时间】: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