title: Re-direct URL to Add Trailing Slash in Sinatra
body: <p>Have my app up on Heroku. Using <a href="https://github.com/tylerhunt/rack-canonical-host" rel="nofollow">rack-canonical-host</a> to handle re-directs from myapp.heroku.com to the canonical domain. My domain registrar handles re-directs from the root domain.</p>
  
  <p>I'm using Octopress (a Jekyll-based framework; Sinatra runs underneath) and <strong>I want to re-direct all URL's <em>without</em> a trailing slash to the trailing-slash variant.</strong> I would do this on the web-server side, but I don't have the ability to do so with Heroku.</p>
  
  <p>I also assume that a 301 re-direct is the best practice for performing this re-direct.</p>
  
  <p>I did have a look through the Sinatra docs but it seems that by default it's optional with the "?" on your routes, however my routes don't have this syntax but still handle with and without cases.</p>
  
  <p>This is my current <code>config.ru</code>:</p>
  
  <pre><code>require 'bundler/setup'
  require 'sinatra/base'
  require 'rack/contrib'
  require 'rack-canonical-host'
  
  # The project root directory
  $root = ::File.dirname(__FILE__)
  
  class SinatraStaticServer &lt; Sinatra::Base
  
    get(/.+/) do
      expires 3600, :public, :must_revalidate
      send_sinatra_file(request.path) {404}
    end
  
    not_found do
      expires 0, :public, :no_cache
      send_sinatra_file('404.html') {"Sorry, I cannot find #{request.path}"}
    end
  
    def send_sinatra_file(path, &amp;missing_file_block)
      file_path = File.join(File.dirname(__FILE__), 'public',  path)
      file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i  
      File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
    end
  
  end
  
  if ENV['CANONICAL_HOST']
    use Rack::CanonicalHost, ENV['CANONICAL_HOST'], ignore: ['media.eatsleeprepeat.net', 'static.eatsleeprepeat.net']
  end
  
  use Rack::Deflater
  
  run SinatraStaticServer
  </code></pre>