Am redirecting urls from a legacy site, which gets me to a url like this:
http://example.com/blog/01/detail
I would like to automatically remove the leading zeros from these urls (seems it doesn't matter how many zeros are in there 001
0001
000001
work) so that the page redirects to:
http://example.com/blog/1/detail
Is there a simple way to go about doing this in django ? (Or, via the .htaccess
redirect?)
url(u'^blog/(?P<object_id>\d+)/detail$',
list_detail.object_detail,
{ 'queryset' : Blog.objects.all(), },
name='blog_detail',)
RewriteRule ^blog-([0-9]+) http://example.com/blog/$1 [R=301]
Would I need some middleware or is there an easy way to do this in the urls.py
file ?
You can fix it by eiditing either the urls.py regex or the .htaccess regex:
'^blog/0*(?P<object_id>\d+)/detail$'
RewriteRule ^blog-0*([0-9]+) http://example.com/blog/$1 [R=301]
Perhaps
url(u'^blog/0*(?P<object_id>\d+)/detail$',
list_detail.object_detail,
{ 'queryset' : Blog.objects.all(), },
name='blog_detail',)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.