title: How do I redirect without www using Rails 3 / Rack?
body: <p>I understand there are a lot of questions that answer this. I'm familiar with <code>.htaccess</code> and <code>nginx.conf</code> methods, but I do not have access to such traditional configuration methods on Heroku.</p>
  
  <p><a href="https://stackoverflow.com/users/123527/simone-carletti">Simone Carletti</a> gave this answer that leverages Rails 2.x Metals, but <strong>I'm using Rails 3</strong> and this isn't compatible:</p>
  
  <p><em><a href="https://stackoverflow.com/questions/1706247/redirect-non-www-requests-to-www-urls-in-rails/1706312#1706312">Redirect non-www requests to www URLs in Ruby on Rails</a></em></p>
  
  <h3>Please note:</h3>
  
  <p>I'm not looking for a simple <code>before_filter</code> in my ApplicationController. I'd like to accomplish a rewrite similar to Simone's. I believe this is job for the webserver or middleware like Rack at the very least, so I'd like to leave this bit out of the actual application code.</p>
  
  <h2>Goal</h2>
  
  <pre><code>redirect                to                  status
  ----------------------------------------------------
  www.foo.com             foo.com             301
  www.foo.com/whatever    foo.com/whatever    301
  </code></pre>
  
  <p><strong>Only</strong> hosts matching <code>/^www\./</code> should be redirected. All other requests should be ignored.</p>