Rails + Heroku + Cloudflare - exclude www as a subdomain

I have a Rails 4 multi-tenant application that uses subdomains with PostgreSQL schemas to β€œtenant” the application. I have the wildcard characters "*" set in Heroku and Cloudflare and the site loads, but it continues to "think" that www is a valid subdomain. Because of this, usually valid links (which work in development) do not work in production, because www.mysite.com/accounts/new, etc. Same as asdf.mysite.com/accounts/new. Is there a way to make my application almost ignore www as a subdomain? I would still like mysite.com to redirect to www.mysite.com, but pretty much just set the subdomain to false when it is www?

To make things a little more specific, in my application_controller application I have code that does the following. Pay attention to the comments. In development mode, redirect_to root_url (subdomain: false) works, but during production, it simply redirects endlessly and does not load. To load it, I simply draw the page, but this does not cause the subdomain to be false.

def load_schema Apartment::Tenant.switch!('public') return unless request.subdomain.present? if current_account Apartment::Tenant.switch!(current_account.subdomain) else redirect_to root_url(subdomain: false) # Works locally, but infinite re-direct in production #render 'welcome/index' # - Renders the page in production, but nothing else works. end end 

Basically, everything works as expected in development mode, but there are apparently some DNS issues in production that need to be smoothed out, but I can't figure out what to do. Any thoughts?

+6
source share
2 answers

I had to delete the name A record in CloudFlare and replace it with the CNAME record. I also added 2 more CNAME entries: one wildcard and one for www.

+1
source

My suggestion is to get www.mysite.com to redirect to mysite.com, where you probably have a main site.

You can either execute it by specifying the hostname of the forwarding service, or by using a specific route with a restriction in the routes.rb .

0
source

Source: https://habr.com/ru/post/988793/


All Articles