404s ending in slash redirect to remove that slash, closes #309

This commit is contained in:
Simon Willison 2018-06-21 08:13:07 -07:00
commit 97ae66ccab
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
2 changed files with 19 additions and 1 deletions

View file

@ -468,8 +468,16 @@ class Datasette:
RowView.as_view(self),
"/<db_name:[^/]+>/<table:[^/]+?>/<pk_path:[^/]+?><as_format:(\.jsono?)?$>",
)
self.register_custom_units()
# On 404 with a trailing slash redirect to path without that slash:
@app.middleware("response")
def redirect_on_404_with_trailing_slash(request, original_response):
print("redirect_on_404_with_trailing_slash", request, original_response)
if original_response.status == 404 and request.path.endswith("/"):
path = request.path.rstrip("/")
if request.query_string:
path = "{}?{}".format(path, request.query_string)
return response.redirect(path)
@app.exception(Exception)
def on_exception(request, exception):