Canonical Host

Added by tylerhunt at October 09, 2009 18:12 Star_small_grayStar_small_grayStar_small_grayStar_small_grayStar_small_gray
GitHub stats:
Magnifier watcher(s)
Arrow_branch fork(s)
Wrench

About

There are often times when you'll need to redirect requests for some domains or subdomains to a single canonical host. This middleware lets you specify the canonical host for your application, and will perform a 301 redirect for all non-canonical requests. One of the most common use cases is to redirect the a subdomain like www to the the root domain, or vice versa.

Usage

In its simplest case, just specify your canonical host.

use CanonicalHost, 'example.com'

You can also pass a block that will be evaluated, and its return value will be used as the canonical host. This is often useful when you're targeting multiple environments.

use CanonicalHost do
  case Rails.env.to_sym
    when :staging then 'staging.example.com'
    when :production then 'example.com'
  end
end
blog comments powered by Disqus