influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.66k stars 464 forks source link

Static page for `www` subdomain #609

Closed veeral-patel closed 5 years ago

veeral-patel commented 5 years ago

Thank you for this project! This may have been answered already, but I searched the previous issues and didn't find anything.

Essentially, what I want is:

www.mysite.com -> render my site's static landing page *.mysite.com -> render the appropriate subdomain

How would I set something like this up?

lcjury commented 5 years ago

I'm not sure if this going to work but:

you can try with subdomain exclusion https://github.com/influitive/apartment#switch-on-subdomain and setting a group and using a constrain for your www routes

constraints :subdomain => "www" do
    resources :something
end
veeral-patel commented 5 years ago

@lcjury thanks! here's my solution:

class WorkspaceConstraint
  def self.matches?(request)
    subdomains = %w{ admin www }
    request.subdomain.present? && !subdomains.include?(request.subdomain)
  end
end

Rails.application.routes.draw do
  constraints WorkspaceConstraint do
    resources :messages
    resources :channels
  end

  constraints :subdomain => 'admin' do
    resources :workspaces
  end

  constraints :subdomain => 'www' do
      # static page routes
  end

  constraints :subdomain => '' do
    # redirect
  end
end

Imagine you're creating Slack, and have: