Skip to content

Instantly share code, notes, and snippets.

@rennex
Last active December 29, 2015 01:39
Show Gist options
  • Save rennex/7595143 to your computer and use it in GitHub Desktop.
Save rennex/7595143 to your computer and use it in GitHub Desktop.
If a request to my Sinatra app would return 404 just because of a trailing slash, redirect to the correct path.
not_found do
# redirect GET and HEAD requests to "/foo/" to "/foo"
if (request.get? || request.head?) && request.path_info[-1] == "/"
newpath = request.path_info.chop
if Sinatra::Application.routes["GET"].find {|re,*other| newpath =~ re }
redirect to(newpath)
end
end
haml :x404
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment