Arslan Ali
Arslan Ali

Reputation: 17812

Using www at 2nd level subdomain ignores the 1st level subdomain in apartment

I have subdomains like: lks.harabiz.com & lkm.harabiz.com, each with its own database tables.

When somebody tries the URL: www.lkm.harabiz.com, the database tables being used are different from lkm's, and those database tables actually belong to www, not to lkm.

There are two solutions to this problem:

  1. Point www.lkm.harabiz.com to simply lkm.harabiz.com.
  2. Make www.lkm.harabiz.com use the database tables that actually belong to lkm, and not to www.

I'm using Rails 5.0.0.1, and the app is deployed through Heroku.

I have looked the DNS stuff, plus stuff on application level, but I'm unable to achieve the desired results.

Upvotes: 0

Views: 512

Answers (5)

deepak raghuwanshi
deepak raghuwanshi

Reputation: 144

Change the apartment gem configuration and add 'www' to exclude_domains in config/initializers/apartment/subdomain_exclusions.rb:

Apartment::Elevators::Subdomain.excluded_subdomains = ['www']

Upvotes: 0

JairoV
JairoV

Reputation: 2104

The way to use those different tables is application specific, so you shouldnt add any DNS interaction code because it will make your code fragile and less portable, NOR modify DNS options in heroku or your DNS entries.

What you need is to make use your desired database based on the host of the HTTP request arriving your app. Look at Rails.application.routes and https://apidock.com/rails/ActionController/AbstractRequest/request_uri to do it. Read about request routing for your framework. (or put here your routing code to get more help)

Upvotes: 0

widjajayd
widjajayd

Reputation: 6263

you can trap from your config/routes.rb file with this code below

Rails.application.routes.draw do
  get '', to: 'lkm_controllers#index', constraints: lambda { |r|
   r.subdomain.present? && r.subdomain == "www.lkm"
  }
  get '', to: 'lks_controllers#index', constraints: lambda { |r|
   r.subdomain.present? && r.subdomain == "www.lks"
  }
  # other resources
end

make sure these 2 commands you put above, so it checked it first whether it's using www.lkm or just lkm

Upvotes: 0

nikolayp
nikolayp

Reputation: 17949

Simply increase Top Level Domain by 1:

# config/environments/production.rb
Rails.application.configure do
  config.action_dispatch.tld_length = 2
end

More information about this option is there: http://guides.rubyonrails.org/configuring.html

And there's a little description about how it works: http://api.rubyonrails.org/classes/ActionDispatch/Http/URL.html

Upvotes: 0

wesley6j
wesley6j

Reputation: 1403

You can use a Custom Elevator:

# application.rb
module MyApplication
  class Application < Rails::Application
    config.middleware.use Apartment::Elevators::Generic, Proc.new { |request| request.host.split('.')[-3] }
  end
end

Upvotes: 2

Related Questions