There was an interesting post on hacker news recently, safeshepherd suddenly fell off the google for their own name. The comments had some good answers especially the first by a google employee.

The gist of it is that if you have https:// and http:// and www.domain.com and domain.com google sees 4 versions of you site. It will pick one of them as the main site each time it ranks your site. This can change every, effecting the ranking, as it looks like duplicated content.

For Sinatra there is a nice bit of middle were called rack-no-www which adds the redirect for you.

Add to you gem file

gem  "rack-no-www",  "~> 0.0.2", :require => 'rack/no-www'

Or just gem install rack-no-www

Usage in a basic Sinatra App (app.rb)

require 'sinatra'
require 'rack/no-www'

use Rack::NoWWW

get '/' do
  'Helloworld'
end

Run with ruby app.rb With web browser view localhost:4567. If that is working then you should find www.localhost:4567 redirects back to localhost:4567