diff --git a/pinaxcon/middleware.py b/pinaxcon/middleware.py new file mode 100644 index 0000000000000000000000000000000000000000..f0a60b0141375dc77a3f145cc3f408f5e49558e0 --- /dev/null +++ b/pinaxcon/middleware.py @@ -0,0 +1,34 @@ +import re +import warnings + +from django import http +from django.conf import settings +from django.utils.deprecation import MiddlewareMixin + +class UnprependWWWMiddleware(MiddlewareMixin): + """ Unprepends www if necessary. """ + + response_redirect_class = http.HttpResponsePermanentRedirect + + def process_request(self, request): + """ + Rewrite the URL based on settings.UNPREPEND_WWW + """ + + unprepend_www = getattr(settings, "UNPREPEND_WWW", False) + + if not unprepend_www: + return + + # Check for a redirect based on settings.UNPREPEND_WWW + host = request.get_host() + must_unprepend = unprepend_www and host and host.lower().startswith('www.') + wwwless_host = host[4:] + redirect_url = ('%s://%s' % (request.scheme, wwwless_host)) if must_unprepend else '' + + path = request.get_full_path() + + # Return a redirect if necessary + if redirect_url or path != request.get_full_path(): + redirect_url += path + return self.response_redirect_class(redirect_url)