rails 3 - one app, multiple domains, how implement

2019-03-16 06:10发布

问题:

Several different domains names point to my app on heroku, for example foo.com and bar.com both point to the app. (We host specialized blog pages, and foo.com is the domain used by our Users who are creating web pages, and bar.com is the 'public facing' domain where the blog pages are.)

All the user-editing pages have Devise authentication, and the "root" on foo.com is the User's dashboard page. And a logged-in user can preview their blog page at foo.com/reviewpage/USERID

each User acount also has a unique "friendly url name" such as "acme-inc-dallas-tx"

On the public-facing web page bar.com (but ONLY this one domain), I need to somehow map

http://bar.com/friendly-url-name to :controller => mycontroller, :action => myaction

I assume that means I need to re-map 'root' on bar.com (but ONLY bar.com) to a method "find_friendly_url" that looks up the appropriate page and displays it.

If that is the right way to proceed... how would I remap 'root' for one and only one domain that points to my app?

回答1:

I usually proceed as follows:

constraints(Subdomain) do
  match "/" => "home#admin"
end

match "/" => "home#standard" 

Or:

match "/" => "home#admin", :constraints => {:subdomain => "admin"}

match "/" => "home#standard" 

Which creates:

/(.:format) {:action=>"admin", :subdomain=>"admin", :controller=>"home"}
root  /(.:format) {:action=>"standard", :controller=>"home"}

The same logic lets you create routes only available to desired subdomains.