summaryrefslogtreecommitdiffstats
path: root/webapp/django/contrib/redirects/middleware.py
diff options
context:
space:
mode:
Diffstat (limited to 'webapp/django/contrib/redirects/middleware.py')
-rw-r--r--webapp/django/contrib/redirects/middleware.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/webapp/django/contrib/redirects/middleware.py b/webapp/django/contrib/redirects/middleware.py
new file mode 100644
index 0000000000..8998c2ce3e
--- /dev/null
+++ b/webapp/django/contrib/redirects/middleware.py
@@ -0,0 +1,27 @@
+from django.contrib.redirects.models import Redirect
+from django import http
+from django.conf import settings
+
+class RedirectFallbackMiddleware(object):
+ def process_response(self, request, response):
+ if response.status_code != 404:
+ return response # No need to check for a redirect for non-404 responses.
+ path = request.get_full_path()
+ try:
+ r = Redirect.objects.get(site__id__exact=settings.SITE_ID, old_path=path)
+ except Redirect.DoesNotExist:
+ r = None
+ if r is None and settings.APPEND_SLASH:
+ # Try removing the trailing slash.
+ try:
+ r = Redirect.objects.get(site__id__exact=settings.SITE_ID,
+ old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
+ except Redirect.DoesNotExist:
+ pass
+ if r is not None:
+ if r.new_path == '':
+ return http.HttpResponseGone()
+ return http.HttpResponsePermanentRedirect(r.new_path)
+
+ # No redirect was found. Return the response.
+ return response