Subdomain not working in Heroku and Namecheap

I am currently building a SaaS application using rails and I want each branch of the company to have its own subdomain. I use the apartment gem to achieve this and it works great locally but it’s a nightmare in production. I can’t seem to get any subdomain to work in Heroku.

Namecheap is the domain’s registrar. The domain has been redirected successfully to the herokuapp but the sub domain can’t seem to resolve unlike the way it works locally.

Any help is appreciated :pensive:

I believe you need to solve this at your DNS configuration. I’m not terribly well versed in this sort of DNS work, but I think you need a wildcard subdomain DNS entry that also points to your app. From there, in your rails app you should be able to inspect the request and see the specific subdomain the request came through.

Hope that helps.

I’ve set up this kind of system with Heroku and it is indeed a little tricky.

Here are the two relevant DNS records for my installation:

The ALIAS record allows practiceflow.co.uk to be resolved to the Heroku app domain.

The CNAME record points all subdomains of practiceflow.co.uk (* is a wildcard) again to the Heroku app, and as Chris says you can then inspect which subdomain has been requested from within Rails, though it sounds like you already have that set up via the apartment gem.

To make your life easier though I would strongly recommend using DNSimple, they make this setup process simple and they are one of the very few DNS providers who have the ALIAS command—without this, people visiting your bare domain (i.e. no ‘www’ or other subdomain) will just get an error which doesn’t look good).

Don’t forget to buy a wildcard SSL certificate to protect all those subdomains! Again, DNSimple was the easiest way I found to buy the certificate and set it up for use with Heroku.

Thanks so much. I finally got it work using namecheap. DNSimple looks good, would most definitely be trying it out.

The issues were multiple. The DNS turned out to be not the problem at all :smile: The major issues were as a result of oversight. I left out some code (require blah blah) required for apartment to work properly and then to worsen it, I was using the heroku open command from the terminal to open the app. That goes straight to the ‘subdomain’.herokuapp.com and not the www.customdomain.com so heroku was having issues with the sub sub domain. Finally figured that out using the heroku logs -t command.

Thanks for the help too, christoomey. Cheers!