使用Flask微框架将旧网址重定向到新网址 [英] Redirecting an old URL to a new one with Flask micro-framework

查看:62
本文介绍了使用Flask微框架将旧网址重定向到新网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask微框架(基于Werkzeug)创建一个新网站来替换当前网站,该框架使用Python(在我的情况下为2.6).

I'm making a new website to replace a current one, using Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).

核心功能和许多页面是相同的.但是,使用Flask可以使许多以前的URL与旧的URL不同.

The core functionality and many pages are the same. However by using Flask many of the previous URLs are different to the old ones.

我需要一种以某种方式存储每个旧URL和新URL的方法,这样,如果用户键入一个旧URL,它们将被简单地转发到新URL,并且一切正常.

I need a way to somehow store the each of the old URLs and the new URL, so that if a user types in an old URL they are simply forwarded to the new URL and everything works fine for them.



有人知道在Flask中是否可行?



Does anybody know if this is possible in Flask?

在此先感谢您的帮助:-)

Thank you in advance for your help :-)

推荐答案

类似的内容应该可以帮助您入门:

Something like this should get you started:

from flask import Flask, redirect, request

app = Flask(__name__)

redirect_urls = {
    'http://example.com/old/': 'http://example.com/new/',
    ...
}

def redirect_url():
    return redirect(redirect_urls[request.url], 301)

for url in redirect_urls:
    app.add_url_rule(url, url, redirect_url)

这篇关于使用Flask微框架将旧网址重定向到新网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆