[racket] web-server dispatch-rules and trailing slashes in URLs

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Mon Sep 24 16:36:41 EDT 2012

With "web-server", does anyone have a preferred way of making 
"dispatch-rules" handle slashes at the ends of URLs?

For example, if I have a URL "/foo", I'd like to also accept "/foo/" for 
the same behavior.

So, I could do something like this:

(define-values (app-dispatch app-url)
   (dispatch-rules
    (()         handle-home)
    (("")       handle-home)
    (("foo")    handle-foo)
    (("foo" "") handle-foo)
    (("bar")    handle-bar)
    (("bar" "") handle-bar)))

But I'd like to canonicalize either "/foo" or "/foo/", and have its 
counterpart do an HTTP permanent redirect to the canonical one.  (Apache 
does this in some cases, for example.)

So, to canonicalize, I could just add to the "dispatch-rules" a bunch of 
rules to procedures like "handle-foo/non-canonical-url", which do 
"(redirect-to URL permanently)".  But I was wondering whether there was 
already a slicker way of doing this.

Neil V.


Posted on the users mailing list.