I Am Making Progress

documenting a new internet venture from day one

Making Sinatra routes insensitive to the trailing backslash


Sorry I haven’t posted in A WHILE. It’s like all things with me once I get out of the habit it is a real chore to get started again. Hopefully this little post will get me back on track.

Simply put, every Sinatra route you ever write should be as such:

get '/hello/?' do

The important thing that you should always do is end your route with ‘/?’. This will make it so that if someone goes to www.yourdomain.com/hello or www.yourdomain.com/hello/ they will get the same result.

I can’t think of any case where you would actually want your routes to be sensitive to whether or not the trailing backslash was there, so start doing this from the beginning. I just had to go through 500 lines of routing code to add it. I’m surprised this isn’t included in every intro Sinatra tutorial, but I haven’t seen it in any of them. In case you are wondering, you don’t need to add this to the url string when you tell Sinatra to ‘redirect’ to another url. This ‘/?’ only applies to ‘get’ or ‘post’ conditions.



Share and Enjoy

  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • RSS
  • Bob Corsaro

    Usually you’ll do this at the proxy layer.

    • When you say “proxy layer” is that or could that be a middleware? I’m the author of this blog, not sure if that’s obvious when I respond to comments on Disqus. I’d like to update this post to make it more useful or at least less dogmatic.

      • Bob Corsaro

        Wow, sorry for slow reply. By proxy layer I mean something like nginx or apache rewrite rules.