Python Flask: 如何批量实现多个URL重定向?

huangapple go评论57阅读模式
英文:

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(&lt;URL&gt;) 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(&lt;URL&gt;) 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 =&gt; subdomain.example.com/example-1
www.example.com/example-2 =&gt; subdomain.example.com/example-2
www.example.com/example-3 =&gt; 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 = {
    &quot;/example-1&quot;: &quot;http://subdomain.example.com/example-1&quot;,
    &quot;/example-2&quot;: &quot;http://subdomain.example.com/example-2&quot;,
    &quot;/example-3&quot;: &quot;http://subdomain.example.com/example-3&quot;
}

# Register a route to handle all the redirects
@app.route(&#39;/&lt;path:path&gt;&#39;)
def redirect_to_new_url(path):
    if path in redirects:
        new_url = redirects[path]
        return redirect(new_url, code=302)
    else:
        return &quot;Page not found&quot;, 404

if __name__ == &#39;__main__&#39;:
    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 = (
        (&#39;https://www.example.com/example-1&#39;, &#39;https://subdomain.example.com/example-1&#39;), 
        (&#39;https://www.example.com/example-2&#39;, &#39;https://subdomain.example.com/example-2&#39;)
    )
    for v in redir:
        if request.url in v[0]:
            return redirect(v[1], code=302)

Source 1: https://www.reddit.com/r/flask/comments/115sdhm/comment/j95q9va/?utm_source=share&amp;utm_medium=web2x&amp;context=3

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.

huangapple
  • 本文由 发表于 2023年2月19日 05:40:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定