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. Create short URL redirects for site urls. by matt.geek.nz 4 years, 3 months ago
  2. Smart append slash middleware by akaihola 5 years, 3 months ago
  3. PermanentRedirectMiddleware by marinho 4 years, 10 months ago
  4. Save disk space by hard-linking multiple Django installations by akaihola 6 years, 1 month ago
  5. RedirectedURLField by stringify 3 years, 3 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?)