APPEND_SLASH alternative

160 views
Skip to first unread message

Mihai Preda

unread,
Jan 3, 2007, 4:41:11 AM1/3/07
to Django developers
Hi,

now APPEND_SLASH works like this: for any URL which doesn't end with a
slash, and whose last segment doesn't contain a dot "." (the dot which
typically indicates the file extension), an HTTP-redirect is generated
to an URL formed from the initial URL with a slash appended.

I'd like to propose an alternative way of handling the 'end-slash'
problem, like this:

If the initial URL doesn't end with a slash, and it is not found in
urlpatterns, a new URL is formed by appending a slash at the end. If
this new URL is found in urlpatterns, then an HTTP-redirect is returned
to this new URL; otherwise the initial URL is processed as usual.

This behavior emulates the serving of filesystem-based files and
directories, where a request for a directory without slash at the end
results in a redirect with the slash appended; but a request for a file
(regardless whether the file has a dot or not) doesn't append a slash.

I consider this way of handling the end-slash to be a better
altervative over the two choices that exist now (APPEND_SLASH=True or
APPEND_SLASH=False), as it somehow merges together these two modes. It
also handles uniformilly URLs that have or don't have a dot in the last
segment, which is good.

Cheers,
Mihai Preda

James Bennett

unread,
Jan 3, 2007, 2:57:50 PM1/3/07
to django-d...@googlegroups.com
On 1/3/07, Mihai Preda <mpr...@gmail.com> wrote:
> If the initial URL doesn't end with a slash, and it is not found in
> urlpatterns, a new URL is formed by appending a slash at the end. If
> this new URL is found in urlpatterns, then an HTTP-redirect is returned
> to this new URL; otherwise the initial URL is processed as usual.

Hm. My first impression is that I like it, mostly for the reasons
you've outlined; I had to turn off APPEND_SLASH when I decided I
didn't want trailing slashes on individual entries on my weblog,
because it would add them regardless of whether the slash-less URL
matched anything.

I also like that you've suggested not doing the redirect unless the
"slashified" URL would actually match something; being able to just
404 immediately on something that won't match is nice.


--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Donny

unread,
Jan 4, 2007, 1:31:51 AM1/4/07
to Django developers
I think it would be nice if the process for implementing your own URL
dispatcher were more obvious / accessible. I have several friends who
have told me they had a want for this, and that it took more effort
than it should have. I haven't had to do it myself yet, but I would
have if I had known there was an established method for doing it.

Mihai Preda

unread,
Jan 4, 2007, 8:56:49 AM1/4/07
to Django developers
I submitted a new middleware (tentativelly named SmartSlashMiddleware)
which implements the described behavior, in ticket #3228.

A couple of minor refactorings which are needed by this class are in
ticket #3224.

I hope somebody can review these patches, and commit them after
eventual fixes.

Reply all
Reply to author
Forward
0 new messages