URL design - why the trailing slash?

5,380 views
Skip to first unread message

Brett Thomas

unread,
Jan 27, 2010, 7:54:50 PM1/27/10
to django...@googlegroups.com
Most of the URLConf examples I run across use a trailing slash on all URLs: www.example.com/profile/

I'm not sure why, but I don't like this. I think it looks nicer without: www.example.com/profile

Are there any performance or security reasons to use the trailing slash in Django? Seems like there could be some quirk with regular expressions that I'm not thinking of...

Thanks --
Brett

Tim Miller

unread,
Jan 27, 2010, 8:11:36 PM1/27/10
to django...@googlegroups.com
Brett Thomas wrote:
> Most of the URLConf examples I run across use a trailing slash on all
> URLs: www.example.com/profile/ <http://www.example.com/profile/>

>
> I'm not sure why, but I don't like this. I think it looks nicer without:
> www.example.com/profile <http://www.example.com/profile>

>
> Are there any performance or security reasons to use the trailing slash
> in Django? Seems like there could be some quirk with regular expressions
> that I'm not thinking of...

I could be wrong but I understand it as more of a webserver issue than a
Django convention.

From: http://httpd.apache.org/docs/2.2/mod/mod_dir.html

"A "trailing slash" redirect is issued when the server receives a
request for a URL http://servername/foo/dirname where dirname is a
directory. Directories require a trailing slash, so mod_dir issues a
redirect to http://servername/foo/dirname/."

Eugene Wee

unread,
Jan 27, 2010, 10:40:55 PM1/27/10
to django...@googlegroups.com
Hi,

On Thu, Jan 28, 2010 at 8:54 AM, Brett Thomas <brettp...@gmail.com> wrote:
> Most of the URLConf examples I run across use a trailing slash on all URLs:
> www.example.com/profile/
>
> I'm not sure why, but I don't like this. I think it looks nicer without:
> www.example.com/profile

I believe that it is just a matter of personal preference: some people
like to always have a slash appended (or never appended) for
consistency. I prefer to append a slash when the page is some kind of
category, but leave it out when it is a "leaf node". (I noticed that
this is what the W3C website does.)

> Are there any performance or security reasons to use the trailing slash in
> Django? Seems like there could be some quirk with regular expressions that
> I'm not thinking of...

I do not know about performance, but I doubt there is a security
issue, otherwise it would not have made sense to make the APPEND_SLASH
setting behave the way it does, i.e., a slash is not appended if the
initial URL (without a trailing slash) can be found in the URLconf.

Regards,
Eugene Wee

Graham Dumpleton

unread,
Jan 28, 2010, 1:12:23 AM1/28/10
to Django users

If you don't have a trailing slash for something which is an internal
node, ie., can have child pages, and you use a relative URL rather
than absolute URL, then the relative URL will be resolved incorrectly
by the browser.

For example, if at '/some/path/' and returned HTML uses 'child.html',
then browser will resolve it as:

/some/path/child.html

if accessed as '/some/path' and returned HTML uses 'child,html', then
browser will resolve it as:

/some/child.html

which isn't want you want.

So, you use trailing slash if you want relative URLs in responses.

Either way, you should use one of the other and not allow both at the
same time. That is '/some/path/' and '/some/path' should not both
work. If you do allow both, then search engines will have multiple
entries for same resource. Also may stuff up caching systems or at
least reduce their efficiency as will keep multiple copies.

Graham

Message has been deleted

kRON

unread,
Jan 28, 2010, 9:09:54 AM1/28/10
to Django users
Nope, no caveat there. You'r perfectly free to design your own URLs as
you like.

Ultimately, means you can also have a conflicting situation where you
decide you don't want to have a trailing `/` but you include URLs from
a 3p django application that do.

Reply all
Reply to author
Forward
0 new messages