Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django RedirectSeoMiddleware
class RedirectSeoMiddleware(object):
"""
Insert before django.middleware.common.CommonMiddleware
"""
def process_request(self, request):
if not request.path.endswith('/'):
try:
view, args, kwargs = resolve(request.path + '/')
kwargs['request'] = request
view(*args, **kwargs)
settings.APPEND_SLASH = True
except Http404:
settings.APPEND_SLASH = False
if request.path == request.path.lower():
return
if settings.APPEND_SLASH:
return redirect(request.path.lower(), permanent=True)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.