Login

RegEx redirect fallback middleware

Author:
bowdengm
Posted:
July 8, 2012
Language:
Python
Version:
1.4
Tags:
regex Redirect /
Score:
0 (after 0 ratings)

Simple middleware to complement the built in redirect middleware app. Add this after the contrib.redirect middleware - this will be fired if a 404 is triggered and the contrib.redirect fails to find a suitable redirect.

Useful if you want to add the redirects into the DB - and/or don't have access to the .htaccess script or whatever HTTP server based redirect machinery your site runs off.

You simply add in regex 'old_path' and 'new_path' entries into the same redirect app, but this will try to do a regex find and replace - i.e.

r = new Redirect() r.old_path = '/my/test/path/([a-z]+)/with/regex/' r.new_path = '/my/result/path/$1/with/regex/' r.save()

this will convert:

/my/test/path/abcdef/with/regex/

into:

/my/result/path/abcdef/with/regex/'

also works with query strings:

/index.php?section=products

old_path = '/index.php/\?section=([a-z]+)'

need to add in the forward slash if ADD_SLASHES = True in your settings, and escape the question mark.

new_path = '/section/$1/'

converts the url to '/section/products/'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.contrib.redirects.models import Redirect
from django import http
from django.conf import settings
import re

class RegExRedirectFallbackMiddleware(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()
        
        
        redirects = Redirect.objects.filter(site__id__exact=settings.SITE_ID)
        for r in redirects:
            try:
                old_path = re.compile(r.old_path, re.IGNORECASE)
            except re.error:
                # old_path does not compile into regex, ignore it and move on to the next one
                continue
                
            if re.match(r.old_path, path):
                new_path = r.new_path.replace('$', '\\')
                replaced_path = re.sub(old_path, new_path, path)
                return http.HttpResponsePermanentRedirect(replaced_path)
            
        # Return the response.
        return response

More like this

  1. simple DomainsAliasMiddleware by matrix 6 years ago
  2. Little middleware that create a facebook user by marinho 7 years, 4 months ago
  3. FirstRun Middleware by TheMysteriousX 3 years, 11 months ago
  4. Effective content caching for mass-load site using redirect feature by nnseva 3 years, 9 months ago
  5. SSL Redirect Middleware and testing by willhardy 6 years, 9 months ago

Comments

Please login first before commenting.