英文:
Python Flask: How to implement multiple URL redirects in bulk?
问题
我知道Flask的redirect(<URL>)
函数,我可以在路由中使用它。
但我有几十个URL需要重定向到不同的网站。我想知道是否有一种方法可以在许多蓝图和路由中实现重定向,而不必编写多个redirect(<URL>)
语句。
我在想是否可以批量提供重定向映射数据给Flask。例如:
www.example.com/example-1 => subdomain.example.com/example-1
www.example.com/example-2 => subdomain.example.com/example-2
www.example.com/example-3 => subdomain.example.com/example-3
英文:
I am aware of Flask's redirect(<URL>)
function that I can use inside routes.
But I have dozens of URLs that I need to redirect to a different website. I was wondering if there is a way I can implement redirects without writing multiple redirect(<URL>)
statements across many blueprints and routes.
I was wondering if I could just supply Flask with my redirect mapping data in bulk. e.g.
www.example.com/example-1 => subdomain.example.com/example-1
www.example.com/example-2 => subdomain.example.com/example-2
www.example.com/example-3 => subdomain.example.com/example-3
答案1
得分: 2
可以使用字典或包含它们的元组列表来执行重定向。
例如:
from flask import Flask, redirect
app = Flask(__name)
# 定义重定向映射的字典
redirects = {
"/example-1": "http://subdomain.example.com/example-1",
"/example-2": "http://subdomain.example.com/example-2",
"/example-3": "http://subdomain.example.com/example-3"
}
# 注册一个路由来处理所有重定向
@app.route('/<path:path>')
def redirect_to_new_url(path):
if path in redirects:
new_url = redirects[path]
return redirect(new_url, code=302)
else:
return "Page not found", 404
if __name__ == '__main__':
app.run(debug=True)
英文:
Yes, you can do redirects with dictionaries or a list of tuples to hold them.
For example:
from flask import Flask, redirect
app = Flask(__name__)
# Define a dictionary of redirect mappings
redirects = {
"/example-1": "http://subdomain.example.com/example-1",
"/example-2": "http://subdomain.example.com/example-2",
"/example-3": "http://subdomain.example.com/example-3"
}
# Register a route to handle all the redirects
@app.route('/<path:path>')
def redirect_to_new_url(path):
if path in redirects:
new_url = redirects[path]
return redirect(new_url, code=302)
else:
return "Page not found", 404
if __name__ == '__main__':
app.run(debug=True)
答案2
得分: 0
cheesecake87
来自Reddit提供了这个解决方案,我会将其标记为被采纳的答案。
from flask import request
@app.before_request
def before_request():
redir = (
('https://www.example.com/example-1', 'https://subdomain.example.com/example-1'),
('https://www.example.com/example-2', 'https://subdomain.example.com/example-2')
)
for v in redir:
if request.url in v[0]:
return redirect(v[1], code=302)
来源1:https://www.reddit.com/r/flask/comments/115sdhm/comment/j95q9va/?utm_source=share&utm_medium=web2x&context=3
来源2:https://stackoverflow.com/a/16121323
Hack-R
的解决方案很不错。但是重定向URL规则被排序到URL规则列表的最底部(由Werkzeug),因此除非我移除我想要重定向的原始路由,否则重定向就不会发生。
有了这个解决方案,URL在到达URL规则列表之前就被评估了。
英文:
cheesecake87
from Reddit provided this solution, which I will mark as accepted answer.
from flask import request
@app.before_request
def before_request():
redir = (
('https://www.example.com/example-1', 'https://subdomain.example.com/example-1'),
('https://www.example.com/example-2', 'https://subdomain.example.com/example-2')
)
for v in redir:
if request.url in v[0]:
return redirect(v[1], code=302)
Source 2: https://stackoverflow.com/a/16121323
Hack-R
's solution was good. But redirection URL rule was sorted to be at the very bottom of the URL rules list (by Werkzeug), so the redirection didn't take place unless I removed the original routes that I wanted redirected.
With this solution, URLs are evaluated BEFORE it reaches the URL rule list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论