2012-05-15 52 views
0

我有一個活的Rails應用程序。現在我想將它們全部重新路由到一個子域dev.mydomain.com。如何將所有路線映射到Rails中的子域

例如,當前路徑mydomain.com/users/1應該變爲dev.mydomain.com/users/1。我的網頁中的所有鏈接也可以正常工作。

我該怎麼做?

非常感謝。

編輯:我想這樣做的原因是因爲我想隱藏我的應用程序,並將它們重定向到不同的登錄頁面。

+0

你搞亂了術語路徑和網址。你想要什麼來存檔? http://apidock.com/rails/ActionView/Helpers/UrlHelper/url_for,看看:主機參數 – astropanic

+0

嗨,我想這樣做的原因是因爲我想隱藏我的應用程序從訪客,並將其重定向到一個不同的着陸頁。 – AdamNYC

+0

'dev.myomain.com'和'mydomain.com'單獨的rails應用程序嗎?你的部署堆棧是什麼? (Apache,nginx,thin,webrick?)。 – Matt

回答

0

你可以做以下

  1. 創建一個名爲subdomain.rb你的config/lib目錄下文件,然後添加該給它

    class Subdomain 
        def self.matches?(request) 
         if request.subdomain == "www" || request.subdomain.blank? || request.subdomain.empty? || request.subdomain.nil? 
          false 
         else 
          true 
         end 
        end 
    end 
    

然後在你的routes.rb你可以這樣做

require 'subdomain' 

    DemoApp::Application.routes.draw do 

     constraints(Subdomain) do 
      constraints(:subdomain => 'dev') do 
       resources :users 
       root :to => "someother#page" 
      end 
     end 

     root :to => "default#index" 

     # and any other routes you would like to expose to www or no subdomain. 
    end 

要運行我的我傾向於使用pow.cx的應用程序,我會推薦相同的應用程序。