I have a multi-tenant Rails app, hosted with Heroku at http://myapp.herokuapp.com. The tenants/accounts are separated via subdomains (and Postgresql schemas). I have then added my own domain as custom domain to the Heroku app. Since Heroku really advice against using A records because of "uptime implications" (https://devcenter.heroku.com/articles/avoiding-naked-domains-dns-arecords) I first tried to put a CNAME record from my custom domain to myapp.herokuapp.com. That worked fine also with subdomains like http://clientaccount.mydomain.com.
The problem began when my client wanted her own domain pointing to their account so that http://www.clientdomain.com showed http://clientaccount.mydomain.com, the most obvious solution seemed to be to create a CNAME record in my clients DNS to point to http://clientaccount.mydomain.com. That did not work, when visiting the address an error message stated that "There is no app configured at that hostname", also host www.clientdomain.comgave:
www.clientdomain.com is an alias for clientaccount.mydomain.com
clientaccount.mydomain.com is an alias for myapp.herokuapp.com
myapp.herokuapp.com is an alias for ar.herokuapp.com
After some VERY confused support from Heroku, they advised my to use A records instead pointed to their three apex IPs. So changed it but it still didn't work. They then told me to add my clients domain as a custom domain in my Heroku settings which I have done without good result.
So my current configuration is as follows:
Myapp at Heroku has following custom domains:
*.mydomain.com www.clientdomain.com
mydomain.com DNS
*.mydomain.com has three A records pointing at Heroku apex IPs
clientdomain.com DNS
In the DNS for clientdomain.com, clientdomain.com (without www) is redirected to www.clientdomain.com (Not sure how they do it but it seems to work.)
For www.clientdomain.com there's a CNAME record pointing at clientaccount.mydomain.com
Current state
www.mydomain.com resolves correctly.
clientaccount.mydomain.com resolves correctly.
www.clientdomain.com goes to www.mydomain.com (without subdomain)
So the problem is obvously either in the DNS somehow, does the subdomain fall away somewhere since my application obviously doesn't receive it? Or do I have to change some code in Rails in order to handle this?
The subdomain is handled as route constraint, Subdomain class:
class Subdomain
def self.matches?(request)
request.subdomain.present? && Account.find_by_subdomain(request.subdomain)
end
end
Any input is appreciated!
EDIT
I have done everything that is suggested, do I have to change my application controller as well?
application_controller.rb
def handle_subdomain
if @account = Account.find_by_subdomain(request.subdomain)
PgTools.set_search_path @account.id
@current_account = @account
else
@current_account = nil
PgTools.restore_default_search_path
end
end