Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

try variants of a URL (without query string, with/without trailing sl…

…ash) when initial redirect lookup fails
  • Loading branch information...
commit b57200fc3734f9387ceba6845c5b4242b2ff7aea 1 parent 32ea835
@nsh-ableton nsh-ableton authored
Showing with 35 additions and 11 deletions.
  1. +35 −11 cms_redirects/middleware.py
View
46 cms_redirects/middleware.py
@@ -2,21 +2,45 @@
from django import http
from django.conf import settings
+
+def get_redirect(old_path):
+ try:
+ r = CMSRedirect.objects.get(site__id__exact=settings.SITE_ID,
+ old_path=old_path)
+ except CMSRedirect.DoesNotExist:
+ r = None
+ return r
+
+
+def remove_slash(path):
+ return path[:path.rfind('/')]+path[path.rfind('/')+1:]
+
+
+def remove_query(path):
+ return path.split('?', 1)[0]
+
+
class RedirectFallbackMiddleware(object):
def process_exception(self, request, exception):
if isinstance(exception, http.Http404):
+
+ # First try the whole path.
path = request.get_full_path()
- try:
- r = CMSRedirect.objects.get(site__id__exact=settings.SITE_ID, old_path=path)
- except CMSRedirect.DoesNotExist:
- r = None
+ r = get_redirect(path)
+
+ # It could be that we need to try without a trailing slash.
if r is None and settings.APPEND_SLASH:
- # Try removing the trailing slash.
- try:
- r = CMSRedirect.objects.get(site__id__exact=settings.SITE_ID,
- old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
- except CMSRedirect.DoesNotExist:
- pass
+ r = get_redirect(remove_slash(path))
+
+ # It could be that the redirect is defined without a query string.
+ if r is None and path.count('?'):
+ r = get_redirect(remove_query(path))
+
+ # It could be that we need to try without query string and without a trailing slash.
+ if r is None and path.count('?') and settings.APPEND_SLASH:
+ r = get_redirect(remove_slash(remove_query(path)))
+
+
if r is not None:
if r.page:
if r.response_code == '302':
@@ -29,5 +53,5 @@ def process_exception(self, request, exception):
return http.HttpResponseRedirect(r.new_path)
else:
return http.HttpResponsePermanentRedirect(r.new_path)
-
+
Please sign in to comment.
Something went wrong with that request. Please try again.