librelist archives

« back to archive

Re: automatic removal of trailing slashes

Re: automatic removal of trailing slashes

From:
William Budd
Date:
2011-02-27 @ 07:41
Hi flask list,

If a user strips a trailing slash from an URL defined with a trailing
slash, the trailing slash gets appended automatically. I like that.
However, if a user appends a trailing slash to an URL defined without
a trailing slash, a 404 is served. I don't like that. This may be
conventional behavior in webdev land, but I would prefer redirection
to said defined URL (i.e. without the trailing slash).

I'm a little obsessive when it comes to minimalism, but from a UI
perspective I don't believe a trailing slash means anything in a HTTP
context, which is why I never have them in my own URLs. It would be
nice if people in my camp got the same level of URL correcting
convenience as the trailing slashers. Are there reasons against having
this feature? Or perhaps there already is a way to enable this as an
option in Flask/Werkzeug?

Will Budd

Re: [flask] Re: automatic removal of trailing slashes

From:
Armin Ronacher
Date:
2011-02-27 @ 11:55
Hi,

On 2/27/11 8:41 AM, William Budd wrote:
> If a user strips a trailing slash from an URL defined with a trailing
> slash, the trailing slash gets appended automatically. I like that.
> However, if a user appends a trailing slash to an URL defined without
> a trailing slash, a 404 is served. I don't like that. This may be
> conventional behavior in webdev land, but I would prefer redirection
> to said defined URL (i.e. without the trailing slash).
Werkzeug follows the behavior of Apache and other webservers there.  You 
can't access 'index.html/', it will result in an 404 error.  Currently 
redirects in the "opposite direction" are not supported but you can tell 
Werkzeug to not care about trailing slashes at all by flipping the 
strict slashes flag:

app.url_map.strict_slashes = False


Regards,
Armin

Re: [flask] Re: automatic removal of trailing slashes

From:
William Budd
Date:
2011-02-28 @ 02:51
Hmm, after giving this a little thought I've abandoned my crusade
against the trailing slash. Since I'm at least as big a sucker for
consistency as I am for minimalism, and since there's no way to get
rid of the trailing slash in "http://website.com/" (RFC 2616 requires
that absolute paths contain at least one slash), I've reluctantly
decided to throw a trailing slash behind everything. I see now that
the Werkzeug default makes perfect sense (in this imperfect world).

Re: [flask] Re: automatic removal of trailing slashes

From:
Simon Sapin
Date:
2011-02-27 @ 07:53
Le 27/02/2011 16:41, William Budd a écrit :
> I'm a little obsessive when it comes to minimalism, but from a UI
> perspective I don't believe a trailing slash means anything in a HTTP
> context, which is why I never have them in my own URLs.

Hi,

I don’t think directly supported by Werkzeug, but if *none* of your URLs 
have a trailing slash (except for the root) it’s easy enough:

@app.before_request
def remove_trailing_slash():
if request.path != '/' and request.path.endswith('/'):
return redirect(request.path[:-1])

Or something like this. Not sure what happens if you app is not at the 
root of its domain, though.

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Re: automatic removal of trailing slashes

From:
William Budd
Date:
2011-02-27 @ 09:25
On Sun, Feb 27, 2011 at 4:53 PM, Simon Sapin <simon.sapin@exyr.org> wrote:
> Le 27/02/2011 16:41, William Budd a écrit :
>> I'm a little obsessive when it comes to minimalism, but from a UI
>> perspective I don't believe a trailing slash means anything in a HTTP
>> context, which is why I never have them in my own URLs.
>
> Hi,
>
> I don’t think directly supported by Werkzeug, but if *none* of your URLs
> have a trailing slash (except for the root) it’s easy enough:
>
> @app.before_request
> def remove_trailing_slash():
> if request.path != '/' and request.path.endswith('/'):
> return redirect(request.path[:-1])

Using the before_request method like that provides a nice workaround.
Thanks a lot.