Home > Web Development > URL Rewrites in Rails 3.0 for Heroku

URL Rewrites in Rails 3.0 for Heroku

I recently built a new Rails 3.0 app for doing phone number lookups (www.whackanumber.com). I decided that for various reasons I was going to try out Heroku since my previous apps have been hosted at EngineYard.

So far so good – look for another post about comparisons between Heroku and EngineYard.

One thing that was difficult (but then easy) was figuring out a way to rewrite from the canonical domain (whackanumber.com) to www.whackanumber.com. There are various opinions about whether this is necessary for search engines as it used to be believed that search engines (Google) would treat content indexed at whackanumber.com and www.whackanumber.com as duplicate content. Regardless, I like it better when I only see one of the above in my site indexes, and so I want to rewrite to www.*

In EngineYard, this was easy using Nginx config, so this:

1
2
3
4
5
server {
  listen 80;
  server_name example.com;
  rewrite ^/(.*) http://www.example.com/$1 permanent;
}

And then using the EngineYard “keep” configs, this was easy:

http://docs.engineyard.com/appcloud/howtos/customizations/make-changes-to-nginx-or-monit-configuration.

However, Heroku’s environment is much more locked down. So no dice on modifying web server config files. Heroku’s DNS docs recommend adding a before_filter to ApplicationController that will ensure the correct domain:

http://devcenter.heroku.com/articles/custom-domains

But I don’t like this approach because this applies to all environments (test, dev, prod, etc.) and I only want this rewrite to be in prod (otherwise it breaks all of my tests).

Instead, I found an article that discusses how to do this rewrite using rack. I have seen postings that have said that the most efficient places to do rewrites in Rails applications are in this order:

1. Web server (can’t do this in heroku, can do it in EngineYard)

2. Rack

3. Rails (via the app)

So Rack is better than using the code suggested by Heroku for two reasons a) it performs better and b) you can specify the configuration by environment (more on this below).

So, steps to use rack are as follows:

1. Install rack-rewrite gem:

a. add rack-rewrite into your gemfile

b. bundle install

2. Add the following lines into your environments/production.rb file:

config.gem 'rack-rewrite'

require 'rack-rewrite'
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r301 /.*/, Proc.new {|path, rack_env| "http://www.#{rack_env['SERVER_NAME']}#{path}" },
:if => Proc.new {|rack_env| ! (rack_env['SERVER_NAME'] =~ /www\./i)}
end

That’s it. Redeploy your app to heroku and every time you type domain.com/whatever you’ll be rewritten to www.domain.com/whatever.

Advertisement
Categories: Web Development Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.