使用Flask微框架将旧网址重定向到新网址

编程入门 行业动态 更新时间:2024-10-22 14:27:33
本文介绍了使用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 = { 'example/old/': 'example/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微框架将旧网址重定向到新网址

本文发布于:2023-10-12 16:57:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   到新   重定向   框架   Flask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!