views:

86

answers:

2

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 code:

url(u'^blog/(?P<object_id>\d+)/detail$', 
    list_detail.object_detail,
    { 'queryset' : Blog.objects.all(), },
    name='blog_detail',)

.htaccess (being redirected):

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 ?

+1  A: 

Perhaps

url(u'^blog/0*(?P<object_id>\d+)/detail$', 
    list_detail.object_detail,
    { 'queryset' : Blog.objects.all(), },
    name='blog_detail',)
Corey Porter
Thanks for the idea - but this isn't actually _changing_ the final url by removing the zeros ... the links work, but I want to get rid of all the leading 0s. ... i want to go from 01 to 1 in the url
thornomad
making the same change to the .htaccess regex will actually remove the 0's in the url
David Claridge
+3  A: 

You can fix it by eiditing either the urls.py regex or the .htaccess regex:

In Django

'^blog/0*(?P<object_id>\d+)/detail$'

In .htaccess

RewriteRule ^blog-0*([0-9]+) http://example.com/blog/$1 [R=301]
David Claridge
thanks - that's the perfect solution! appreciate it.
thornomad