简体   繁体   English

Flask 中不区分大小写的路由

[英]Case insensitive routing in Flask

Say I have people accessing routes, like:假设我有人访问路线,例如:

helloworld.com/APP
HELLOWORLD.COM/APP
helloworld.com/app

Only the 3rd link works.只有第三个链接有效。 The first and second one give me a 404.第一个和第二个给我一个 404。

How do I configure Flask to have case-insensitive routing, so that people can access my routes even if they type HeLloWoRLd.com/aPp ?如何将 Flask 配置为不区分大小写的路由,以便人们即使输入HeLloWoRLd.com/aPp也可以访问我的路由?

See this thread for a hackish way to do it, and also why it's probably not a good idea.请参阅此线程以了解执行此操作的黑客方法,以及为什么这可能不是一个好主意。

In short: URLs are case-sensitive by W3C standard , and making them case-insensitive will likely mess with search engine indexing of your site among other potential problems.简而言之:根据W3C 标准,URL 区分大小写,使它们不区分大小写可能会扰乱您网站的搜索引擎索引以及其他潜在问题。

The recommended alternative approach is to code a custom 404 error handler which looks for the lower-case version of a mistyped URL and then redirects as appropriate.推荐的替代方法是编写一个自定义 404 错误处理程序,它会查找错误输入的 URL 的小写版本,然后根据需要进行重定向。

Also note that the domain name itself isn't under flask's routing control anyway (and is actually case-insensitive already) -- so only the /app part matters.还要注意的是域名本身并不瓶的路由控制下,反正(和实际上不区分大小写的话) -所以只能/app的部分事项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM