Skip to content

Instantly share code, notes, and snippets.

@bpicolo
Last active August 23, 2016 02:38
Show Gist options
  • Save bpicolo/0de3f22d4e52f8ad11b16886825c85be to your computer and use it in GitHub Desktop.
Save bpicolo/0de3f22d4e52f8ad11b16886825c85be to your computer and use it in GitHub Desktop.
A python Pyramid tween to redirect routes that end in a slash to not end in a slash
# -*- coding: utf-8 -*-
from pyramid.httpexceptions import HTTPMovedPermanently
from urllib.parse import urlunparse
from urllib.parse import urlparse
def trailing_slash_tween(handler, registry):
"""Redirect any route ending with a / to not have a slash."""
def tween(request):
if not request.path.endswith('/') or request.path == '/':
return handler(request)
return HTTPMovedPermanently(
urlunparse(urlparse(request.url)._replace(path=request.path[:-1]))
)
return tween
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment