简体   繁体   中英

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',)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM