- January 21, 2012
- permanent redirect no slashes no slash
- 0 (after 0 ratings)
The canonical notion of urls ending in slashes dates from a web where urls were used to access documents and files this is no longer the case so keeping your urls witouth trailing slashes makes them prettier. The problem is that many people/blogs/spiders/browsers could end up with a url with slashes which can be problematic for you SEO, or confuse users.
This script is for sites with no trailing slash dicipline in their urls, and to prevent everybody getting a horrible 404 for a simple slash you just got to remove it and issue a permanent redirect (301) and you'll get your pretty urls your cake and eat it too.
I must stress, you will have to edit all your public urls removing the slashes like so:
url(r'^login$', login,} If you forget, to edit them and visit the url, your browser will remember the redirect and you'll have to clean the browsing history to fix it.
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
- Smart append slash middleware by akaihola 7 years, 7 months ago
- Create short URL redirects for site urls. by matt.geek.nz 6 years, 7 months ago
- Strip trailing .html extensions from URLs so that existing bookmarks work for a legacy site ported to Django by daverowell 8 years, 4 months ago
- SSL Redirect Middleware and testing by willhardy 7 years, 2 months ago
- HTTPS redirections middleware with updated URL template tag by xlq 2 years, 11 months ago