Flask route without trailing slash returns 404 when slash is appended — how should both variants be handled?
0 reputation · 18 Apr 2023, 16:48 UTC
I am defining URL rules in a Flask application using the standard Werkzeug-based routing system. I noticed that the behavior differs depending on whether the route decorator includes a trailing slash: a route defined as /about/ redirects requests for /about to the slashed version, while a route defined as /about returns a 404 when /about/ is requested.
My goal is to pick a consistent convention for a project that sits behind a reverse proxy, and I am unsure how the redirect and 404 behavior interacts with proxies that may normalize or strip trailing slashes before forwarding the request. I am working with a current Flask 3.x release and default Werkzeug routing, with no custom URL converters.
Is the redirect from the non-slashed to the slashed form a permanent (301) redirect that clients and caches will store long-term? If the proxy strips trailing slashes, could the slashed-route redirect create a redirect loop? Is there a documented, supported way to make both /about and /about/ serve the same view without issuing a redirect?