RegEx redirect fallback middleware
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/'
- regex
- Redirect
- /