Redirect to no slash

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#mymiddleware.py
class RedirectToNoSlash(object):
    def process_request(self,request):
        if '/admin' not in request.path and request.path != '/':
            if request.path[-1] == '/':
                return HttpResponsePermanentRedirect(request.path[:-1]) 

#settings.py
MIDDLEWARE_CLASSES = (
    'mymiddleware.RedirectToNoSlash',
    ...            
)

#Make sure its the first middleware, or its before any url manipulating middleware such as CommonMiddleware.

More like this

  1. Smart append slash middleware by akaihola 6 years, 2 months ago
  2. Create short URL redirects for site urls. by matt.geek.nz 5 years, 2 months ago
  3. Strip trailing .html extensions from URLs so that existing bookmarks work for a legacy site ported to Django by daverowell 6 years, 11 months ago
  4. SSL Redirect Middleware and testing by willhardy 5 years, 9 months ago
  5. HTTPS redirections middleware with updated URL template tag by xlq 1 year, 6 months ago

Comments

klemens (on January 23, 2012):

isn't that obsolet?

https://docs.djangoproject.com/en/dev/ref/settings/#append-slash

#

(Forgotten your password?)