Case insensitive urls

721 views
Skip to first unread message

Ramashish Baranwal

unread,
Jun 1, 2007, 9:35:29 PM6/1/07
to Django users
Hi,

I would like to make urls of my site case-insensitive. As I
understand, by default the url matching is case-sensitive. Is there a
way to specify in my urls.py that I want a case-insensitive match?

Thanks,
Ram

SmileyChris

unread,
Jun 1, 2007, 9:43:05 PM6/1/07
to Django users
Hi Ramashish,

I haven't tried it, but I think that you could just use regular
expression extension notation to make the regex case insensitive in
your url conf.
So instead of:
('users/add/', 'views.add'),
You'd do:
('(?i)users/add/', 'views.add'),

Try it and let everyone know if that works...

On Jun 2, 1:35 pm, Ramashish Baranwal <ramashish.li...@gmail.com>
wrote:

sansmojo

unread,
Jun 1, 2007, 9:56:35 PM6/1/07
to Django users
Just pull your regular expression into a variable and use re.I or
re.IGNORECASE. Fore example:

urls.py:
from django.conf.urls.defaults import *

import re
re_admin = re.compile(r'^admin/', re.I)

urlpatterns = patterns('',
(re_admin, include('django.contrib.admin.urls')),
)

This will allow a match for any case: /admin/, /aDmiN/, /ADMIN/

Bryan Chow

unread,
Jun 2, 2007, 10:35:38 PM6/2/07
to Django users
URLs should be unambiguous, i.e any logical piece of content should
have one and only one definitive URL, with any alternatives acting as
a permanent redirect.
http://simonwillison.net/2007/Feb/4/urls/

Therefore, rather than configuring urls.py to ignore case, a better
solution would be to use mod_rewrite to map the "wrong case" URLs to
the correct ones.

For example, you could configure mod_rewrite to convert all URLs to
lowercase.

RewriteEngine on
RewriteMap lowercase int:tolower
RewriteRule ^/(.*)$ /${lowercase:$1}

Hope this helps.

Bryan


On Jun 1, 6:35 pm, Ramashish Baranwal <ramashish.li...@gmail.com>
wrote:
> Hi,
>
> I would like to makeurlsof my sitecase-insensitive. As I
> understand, by default the url matching iscase-sensitive. Is there a

Reply all
Reply to author
Forward
0 new messages