from django.conf import settings
from django import http
from django.utils.cache import patch_response_headers
class ForceCanonicalHostnameMiddleware(object):
def process_request(self, request):
"""Modified common middleware for Conservancy site
@@ -16,13 +16,13 @@ class ForceCanonicalHostnameMiddleware(object):
# Never allow connection to the /admin part of the site without SSL
if (not request.is_secure) and request.path.startswith('/admin'):
url = 'https://sfconservancy.org%s' % request.path
return http.HttpResponseRedirect(url)
# Check for a redirect based on settings.APPEND_SLASH
host = http.get_host(request)
host = request.get_host()
old_url = [host, request.path]
new_url = old_url[:]
# Append a slash if append_slash is set and the URL doesn't have a
# trailing slash or a file extension.
if settings.APPEND_SLASH and (old_url[1][-1] != '/') and ('.' not in old_url[1].split('/')[-1]):
new_url[1] = new_url[1] + '/'