Skip to main content
 首页 » 编程设计

python之如何在遗留 Flask 应用程序中实现个性化 URL

2025年02月15日13Renyi-Fan

我遇到了一个问题,我需要将旧版 Flask 应用程序中的现有 URL 重定向或替换为更“虚荣”的 URL 方案。

例如:

www.example.org/camp -> 确实指向 https://example.org/connect/rally_camps/register

虽然我设法使用 nginx 配置完成这项工作(这是使用典型的 uwsgi + 反向代理 nginx 配置作为 serverd):

location /camp { 
       rewrite ^/.* https://example.org/connect/rally_camps/register permanent; 
} 

当我点击实名网址时,我被重定向到非实名网址(长网址)。这显然看起来很难看......我不确定是否有办法告诉 nginx 重定向但保持相同的 URL 或者这需要一些 Flask 工作......当用户点击虚 URL 到长 URL 时 301 重定向可能?但我认为这会再次更改 URL...有什么想法吗?

谢谢!

请您参考如下方法:

假设您不需要捕获 /camp/ 之后的任何内容,这个 nginx 配置应该可以做到:

location /camp { 
       rewrite ^/.* /connect/rally_camps/register ; 
} 

来自nginx docs for rewrite :

If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client.

换句话说,如果您不想重定向,则替换字符串不能以这些前缀中的任何一个开头。

permanent 标志也会产生重定向,因此您也不能使用它。