title: Multi-tenant rails application with a domain/subdomain architecture like Shopify
body: <p>I'm building a multi-tenant rails application, similar to Shopify. Whenever a customer registers an <code>Account</code>, they will have a subdomain created at <code>customer.myapp.com</code>. This subdomain returns a view with data related to their account (including a /admin area).</p>
  <p>Now in some cases, customers would like to use their own custom domain, instead of the subdomain created for them. How do I need to adjust my rails routes and controllers to return a view with data related to the customers account, based on not just the subdomain but on either the custom domain OR the subdomain?</p>
  <p>This is how I've set up my <code>config/routes.rb</code> for handling subdomains:</p>
  <pre><code># config/routes.rb
  Rails.application.routes.draw do
    ...
    constraints(SubdomainRequired) do
      scope module: :accounts do
        ...
      end
    end
  end
  </code></pre>
  <p>With <code>app/constraints/subdomain_required.rb</code> looking like this:</p>
  <pre><code># app/constraints/subdomain_required.rb
  class SubdomainRequired
    def self.matches?(request)
      request.subdomain.present? &amp;&amp; request.subdomain != &quot;www&quot;
    end
  end
  </code></pre>
  <p>And finally my <code>app/controllers/accounts/base_controller.rb</code> setting the current account, based on the requests subdomain:</p>
  <pre><code># app/controllers/accounts/base_controller.rb
  module Accounts
    class BaseController &lt; ApplicationController
      before_action :set_current_account
      ...
      def set_current_account
        if request.subdomain.present?
          @current_account = Account.find_by(subdomain: request.subdomain)
          render_404 if !@current_account
        end
      end
  
      def render_404
        render file: &quot;#{Rails.root}/public/404.html&quot;, status: 404
      end
      ...
    end
  end
  </code></pre>
  <p>For the DNS setup I followed the approach of companies like Shopify. It looks like they have their customers create an A record that points to the primary IP address of their app. And they have them create a CNAME for the &quot;www&quot; subdomain, which points to a subdomain (<code>sites.myapp.com</code>), set up as a CNAME for the apps subdomain (in my case managed by DigitalOcean's app platform).</p>
  <p>IF the DNS Settings work as intended, I'm left wondering how I can point requests in the right direction when the customer lands on my app. So for example if traffic hits <code>customer.com</code> and it has an A record pointing to my servers primary IP, plus a CNAME for <code>sites.myapp.com</code>, how do I handle the request and redirect it to the correct subdomain whilst keeping the user on their custom domain?</p>
  <p>These are the resources I've checked so far:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/49086741/laravel-application-with-subdomains-that-can-be-linked-to-other-domains-via-cnam">Laravel application with subdomains that can be linked to other domains via CNAME</a></li>
  <li><a href="https://stackoverflow.com/questions/6821755/multi-tenant-with-custom-domain-on-rails">multi tenant with custom domain on rails</a></li>
  <li><a href="https://stackoverflow.com/questions/57931882/multi-tenant-dynamic-multiple-domains-with-ssl-pointing-to-single-host-cdn">Multi-tenant dynamic multiple domains with SSL pointing to single Host/CDN</a></li>
  <li><a href="https://answers.netlify.com/t/subdomain-and-custom-domain-based-multi-tenant-application/30482/2" rel="nofollow noreferrer">https://answers.netlify.com/t/subdomain-and-custom-domain-based-multi-tenant-application/30482/2</a></li>
  <li><a href="https://www.indiehackers.com/post/how-do-i-setup-a-domain-to-subdomain-architecture-like-shopify-70413820da" rel="nofollow noreferrer">https://www.indiehackers.com/post/how-do-i-setup-a-domain-to-subdomain-architecture-like-shopify-70413820da</a></li>
  <li><a href="https://help.shopify.com/en/manual/online-store/domains/add-a-domain/using-existing-domains/connecting-domains#set-up-your-existing-domain-to-connect-to-shopify" rel="nofollow noreferrer">https://help.shopify.com/en/manual/online-store/domains/add-a-domain/using-existing-domains/connecting-domains#set-up-your-existing-domain-to-connect-to-shopify</a></li>
  </ul>
  <p>Thanks for your help!</p>