Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive routing in Flask

Tags:

python

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.

How do I configure Flask to have case-insensitive routing, so that people can access my routes even if they type HeLloWoRLd.com/aPp?

like image 542
unclelim12 Avatar asked Dec 02 '22 17:12

unclelim12


1 Answers

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.

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.

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.

like image 65
tzaman Avatar answered Dec 05 '22 07:12

tzaman