Flask 301 Response Flask 301 Response nginx nginx

Flask 301 Response


The traceback shows that it was the route matching that raised a redirect; usually (e.g. unless you added explicit redirect routes), that means the client tried to access a branch URL (one that ends with a trailing slash), but the requested URL did not include the last slash. The client is simply being redirected to the canonical branch URL with the slash.

From the Werkzeug Rule documentation:

URL rules that end with a slash are branch URLs, others are leaves. If you have strict_slashes enabled (which is the default), all branch URLs that are matched without a trailing slash will trigger a redirect to the same URL with the missing slash appended.

From the routing documentation:

Flask’s URL rules are based on Werkzeug’s routing module. The idea behind that module is to ensure beautiful and unique URLs based on precedents laid down by Apache and earlier HTTP servers.

Take these two rules:

@app.route('/projects/')def projects():    return 'The project page'@app.route('/about')def about():    return 'The about page'

Though they look rather similar, they differ in their use of the trailing slash in the URL definition. In the first case, the canonical URL for the projects endpoint has a trailing slash. In that sense, it is similar to a folder on a file system. Accessing it without a trailing slash will cause Flask to redirect to the canonical URL with the trailing slash.

In the second case, however, the URL is defined without a trailing slash, rather like the pathname of a file on UNIX-like systems. Accessing the URL with a trailing slash will produce a 404 “Not Found” error.

This behavior allows relative URLs to continue working even if the trailing slash is ommited, consistent with how Apache and other servers work. Also, the URLs will stay unique, which helps search engines avoid indexing the same page twice.

As documented, if you do not want this behaviour (and have the url without the trailing slash be a 404 Not Found instead), you must set the strict_slashes=False option on your route.