I was Wondering how to use my Rails application to redirect to two URLs .My application should be used on two domains with one server and one database at my back end with single application. I faced this scenario when i was working for an onsite project.
going into details…
when we hit on domain1 say http://www.domain1.com the app should redirect to controller => :home, action => :home an when we hit on domain2 say http://www.domain2.com the app should redirect to controller => :account, :action => :login. This is the actual scenario that we were looking
The major part of the code handles with Routes.rb and Environment.rb.
First write a method in application.rb/application_helper.rb to deferentiate any css/images/links visible on that domains
let us say in application.rb the code looks like
# check Whois Domain Name
# Domain name will be domain1.com or domain2.com based on the request
def check_domain?
if request.domain == “domain1.com”
login_required
elsif request.domain ==”domain2.com”
# login not required
end
and finally add the code in your application to differntiate URls
# in config/routes.rb map '', :controller => 'domain_tests', :action => 'domain1', :conditions => {:domain => "domain1"} map '', :controller => 'domain_tests', :action => 'domain2', :conditions => {:domain => "domain2"} # in config/environment.rb after the initialize section {:method => request, :domain => request('.') } end end alias_methodld_recognition_conditions, :recognition_conditions result = old_recognition_conditions result << "conditions[:hostname] === env[:hostname]" if conditions[:hostname] result end end end end # courtesy of http://www.smallroomsoftware.com/articles/2007/2/10/rails-routing-based-on-hostname # in app/domain_tests_controller.rb render :text => "you've hit domain one" end render :text => "you've hit domain two" end Finally we are done and this works in Production pretty good !!! Test it in your app's and get me a feedback ..ciao