Ronny Lam

about://tech

Moving to the Root With Rack-rewrite

Now that my blog is on Rails I can do a couple of nice things that I couldn’t do with other blog-platforms. I have been using “blog.ronnylam.nl” for a couple of years now, but am not doing anything with the root-domain “ronnylam.nl”. Today I thought it was about time to move my blog to the root-domain and redirect “www” and “blog” with a 301 permanent redirect to the root.

For that I used Rack::Rewrite, but because I have been troubleshooting a Heroku application error I want to share the setup. In the octopress directory I did:

1
2
echo "gem 'rack-rewrite', '~> 1.3.3'" >> Gemfile
bundle install

Then I put the following in config.ru:

1
2
3
4
5
6
7
8
9
10
11
require 'bundler/setup'
require 'rack/rewrite'
require 'sinatra/base'

# The project root directory
$root = ::File.dirname(__FILE__)

use Rack::Rewrite do
  r301 %r{.*}, 'http://ronnylam.nl$&', :if => Proc.new 
  {|rack_env| rack_env['SERVER_NAME'] != 'ronnylam.nl' }
end

This sends a 301 to everyone that goes to something else then ronnylam.nl. But the nice thing is that it rewrites the URL to the original directory the reader was going to.

Of course a git push heroku master puts it all in the cloud.