Luke Keller
Luke Keller

Reputation: 2556

Rails Constraints and Heroku Sub Subdomains

I have a Rails app I'm testing that uses constraints to define a subdomain (cgc) for certain routes. Locally, everything works as expected. Pushing my code to the staging app on Heroku however, the subdomain doesn't work.

I've tried setting up CNAME records so that staging.our-website.com points to our-app.herokuapp.com, and another CNAME record for cgc.staging.our-website.com that also points to our-app.herokuapp.com. Both of these domains have also been added to the app itself in Heroku, and both are routing to the index of the application. The end result I would like is for the "cgc" subdomain to be handled by Rails so that `cgc.staging.our-website.com goes to the intended pages.

If all goes well in staging, the final home for the apps will be our-website.com and cgc.our-website.com respectively - not sure if this impacts the potential solutions.

Upvotes: 2

Views: 851

Answers (1)

pnomolos
pnomolos

Reputation: 886

I'm not sure how you have your routing set up, but if you do something like the following:

constraints(subdomain: /^cgc(\.|$)/) do
    # Any subdomain-only matches here
end

Then you'll match both 'cgc' as subdomain, as well as 'cgc.whatever-in-here', which should meet your needs :)

The problem is that Rails treats everything before the root domain name as the subdomain, rather than just the first portion. I'm not sure which is technically correct.

Followup:

In order to fix the problem I noted about (about Rails and subdomain handling), you may want to look at the

config.action_dispatch.tld_length

setting. Increasing it beyond 1 means that additional subdomain portions get treated as part of the main domain for the purpose of URL generation. Note that this will probably affect how the "subdomain" matcher works, but I haven't verified that.

Upvotes: 1

Related Questions