开发者

Can I remove leading zeros in a url in django?

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 ?


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]


Perhaps

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜