I've got a handful of sub-domain names on my small rails application, and also the primary domain too.

Allows say I've got a login route such as this:

 match "login", :controller => "user_periods", :action => "new"

This route could be utilized on all domain names and sub-domain names, e.g. :




My real question is how do you pressure a redirect to get rid of the subdomain (or add it). Therefore if someone visits /login on admin.porkjerkyicedcream.com/login they're rerouted towards the primary domain (or the other way around)?



  1. I do not always require the solution within the routing.

  2. I wish to avoid indicating and domain title within the application itself so It may be run it plenty of places on many different domain names (just like a different dev domain)

Use a before_filter inside your controller after which redirect properly. For instance:

class UserSessionsController < ApplicationController

  before_filter :ensure_proper_subdomain, :only => "new"

  def ensure_proper_subdomain

    if request.host_with_port != 'admin.porkjerkyicedcream'

      redirect_to params.merge()




I do not think this is often completed in routing file. But regardless, you could create a custom filter and redirect customers based on the current domain/path.

I believe you can manage this in application_controller should you add:

  def default_url_options(options=)

    #add whatever logic you would like