Hosting multiple domains from a single Rails app

January 21, 2009

January 21, 2009

[wpseo_breadcrumb]

Categories

Not long ago, we decided to create an additional domain for a new tool on our website. This tool was sufficiently unique that we wanted to give it its own identity… while still tying it into the TransFS.com branding. Obviously, we could have just created a new Rails app and coded up a website with […]

46 Seconds to Discover Thousands of Wasted Dollars in your Business.

82% of companies find $12,000 wasted annually. How much are you losing?

nexus-feefighters-quiz-saving-money
Not long ago, we decided to create an additional domain for a new tool on our website. This tool was sufficiently unique that we wanted to give it its own identity… while still tying it into the TransFS.com branding. Obviously, we could have just created a new Rails app and coded up a website with links back to TransFS… but why do all that extra work? We wanted a way to host this new domain directly from our existing rails app.

Some of our reasons for doing this:

  • Simplify the backend: server maintenance, releases, and hosting costs
  • Repurpose existing CSS, images, javascript, etc
  • Repurpose existing code (avoid complications of git submodules keeping shared code in sync between separate Rails apps)

However, we quickly realized that Rail’s doesn’t really support hosting a separate domain from the same app… at least, not out of the box. What we needed was the ability to use routing rules to specify that requests coming from our new domain, truecostofcredit.com, be directed to specific a Controller.

Fortunately, we came across a couple of extremely useful blog posts that helped solve this problem for us.
First, Jamis Buck gives this excellent clinic on the guts of Rails routing: Under the hood: route recognition in Rails
The most helpful resource for us was this: Rails routing based on hostname

We were able to adapt the code used in the above link for our purposes… allowing us to create custom routes that were conditioned on the domain name. Here’s what ended up in one of our initializers:

#
# This allows us to set up custom routes that depend on the domain or host of the request, ie:
# map.connect '', :controller => 'blah', :action => 'blah', :conditions => {:domain => 'blah'}
#
 
module ActionController
  module Routing
    class RouteSet
      def extract_request_environment(request)
        env = { :method => request.method }
        env[:domain] = request.domain if request.domain
        env[:host] = request.host if request.host          
        env
      end
    end
    class Route
      alias_method :old_recognition_conditions, :recognition_conditions
      def recognition_conditions
        result = old_recognition_conditions
        result << "conditions[:domain] === env[:domain]" if conditions[:domain]
        result << "conditions[:host] === env[:host]" if conditions[:host]        
        result
      end
    end
  end
end

Then, we could use this in our routes.rb in the following way:

  map.connect '', :controller => 'true_cost_calculator', :action => 'new', :conditions=>{ :domain=>'truecostofcredit.com' }

Thus… when someone visits truecostofcredit.com, they are directed to our TrueCostCalculatorController!

We loved this solution. It saved us a ton of time and effort, and kept our codebase nice and clean.

Reduce Credit Card Processing Fees

with FeeFighters

[comments-template]

Recent Posts

In today’s business world, the point of sale (POS) system has become a key part of operations for retailers. The POS system is involved in several aspects of a business, such as inventory management, funds, and managing the inflow and outflow of products and services within a store. Due to its unique position in key […]

What Is Sales Tax Compliance Outsourcing? (SALT)Ever since the South Dakota v. Wayfair court ruling that you don’t need to have a presence in a state in order to be required to collect taxes on sales there, tax law has never quite been the same. If you need to collect taxes in many states and […]

Knowing how to properly collect sales taxes in different states and jurisdictions is incredibly complicated. Different tax jurisdictions have different laws and rates, but accountants and tax lawyers are expensive. Worse yet, a tax mistake due to ignorance of sales tax law can lead to an audit or penalty that will cost your business a […]

Chargebacks are debit or credit card charges that have been disputed by customers. Sometimes these come in the form of chargeback fraud, but other times they are legitimate. When a chargeback happens, you have to deal with the acquiring bank and other entities in order to dispute it. Regardless of the reason for any given […]

© 2019 – FeeFighters, LLC – Call Us – (646) 448-8804

PO BOX 1459, MOUNTAINSIDE, NJ 07092

×