Last active
August 23, 2016 02:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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