Remove Trailing Slashes in Your Rails App

Aside from being inelegant, having your pages accessible both with and without a trailing slash is an SEO no-no because search engines can treat them as separate pages, diluting your page rank. For this reason, it’s best to choose one and redirect the other to it.

I prefer no trailing slashes for many reasons. First, who wants unnecessary characters? Also, trailing slashes are historically used for directories in a filesystem and left off for individual files. Because every page on the internet is a file (most often an HTML file), semantically trailing slashes don’t belong. Finally, Rails in particular makes it easy to let pages respond to different formats by appending “.json” or “.xml” to the URL, and “/books/.json” looks dumb.

There are plenty of ways to redirect trailing-slash URLs to their slashless counterparts at the server level (via Apache or Nginx), but for someone like me on Heroku with limited access to the server configuration, the rack-rewrite middleware provides a simple solution:

# Gemfile
gem 'rack-rewrite'

# config/application.rb
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
  r301 %r{^/(.*)/$}, '/$1'

Thanks to Nance’s Kitchen for this one.