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
- Django Settings Assignment Expressions aka Walrus Operator example by webology 3 weeks, 3 days ago
- codigo alto nivel by MrRocklion 2 months, 3 weeks ago
- Load template from specific app by Krzysiek555 3 months, 1 week ago
- PostgreSQL JSON subqueries by dolamroth 3 months, 1 week ago
- "Magic Link" Management Command by webology 8 months, 2 weeks ago