Friday, January 15, 2010

Rack::DomainRedirect v0.1.1 released

Just a quick announcement about Rack::DomainRedirect v0.1.1 release.

Rack::DomainRedirect is a tiny rack middleware for redirecting to the configurable domain. If user request’s the service from domain other than that configured it redirects to the first domain on the configuration list. If no domain is configured it does noting and passes requests to another application in the chain.

Read more about it on Rack::DomainRedirect project page where you can read more, see examples, download the code and get instructions on how to install it using ruby gems.

Wednesday, January 6, 2010

Redirecting in rack

I had my CV on apache once, with few domains such as, etc pointing at it which were all redirected by apache's mod_redirect to the with a few simple lines:

# Rewrite rules
RewriteEngine on
RewriteCond %{HTTP_HOST} !^cv\.misza\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$1 [L,R=301]

Recently I moved it to and I didn't have any control over the web server any more.
Fortunately there is Rack - interface between web server and my small sinatra cv application. It looked like the best place to put redirection logic. Here is a piece of rack middleware doing it.

module Rack
# Redirects to correct domain
class RedirectToDomain

HOST = ''

def initialize(app)
@app = app

def call(env)
req =

# localhost for local development
if == HOST or == 'localhost'
res =

To use it with your application require the file above and add

use Rack::RedirectToDomain

in the same way as you would use any other rack middleware. And then the magic happens and you have redirection working without polluting your app with this low level redirection stuff.

You can clone the code on github.