2010-05-01 98 views
8

我有一個網站有多個子域名,我希望命名的子域名robots.txt與www的不同。多個robots.txt在rails中的子域名

我試過使用.htaccess,但FastCGI沒有看它。

所以,我試圖建立的路線,但它似乎並不認爲你不能做直接重寫每一個途徑,因爲需要一個控制器:

map.connect '/robots.txt', :controller => ?, :path => '/robots.www.txt', :conditions => { :subdomain => 'www' } 
map.connect '/robots.txt', :controller => ?, :path => '/robots.club.txt' 

什麼將是最好的方式解決這個問題?

(我用的子域request_routing插件)

+0

「on」不是標籤。 – 2010-05-09 01:18:33

回答

17

其實,你可能想在mime_types.rb設置MIME類型,並做一個respond_to塊,因此不會將其返回爲'text/html'

Mime::Type.register "text/plain", :txt 

然後,你的路線是這樣的:

map.robots '/robots.txt', :controller => 'robots', :action => 'robots' 

對於Rails3中:

match '/robots.txt' => 'robots#robots' 

和(在任何你想放的文件(S))的控制器是這樣的:

class RobotsController < ApplicationController 
    def robots 
    subdomain = # get subdomain, escape 
    robots = File.read(RAILS_ROOT + "/config/robots.#{subdomain}.txt") 
    respond_to do |format| 
     format.txt { render :text => robots, :layout => false } 
    end 
    end 
end 

在過度設計它的風險,我甚至可能被誘惑緩存文件的讀取操作...

哦,是的,你幾乎肯定必須刪除/移動現有的'public/robots.txt'文件。

細心的讀者會發現,你可以很容易地替換爲RAILS_ENV ... subdomain

+3

感謝發佈這個,這啓發了我 - 我簡化了這個技術一點點,並更新了Rails 3. http://www.timbabwe.com/2011/08/rails-robots-txt-customized-by-environment-automatically/ – tkrajcar 2011-08-30 19:14:56

+0

謝謝我喜歡你的解決方案。我已經發布了Rails 3.x解決方案,並稍作修改。 – 2012-12-20 20:18:02

0

如果您不能配置您的服務器的HTTP請求前要做到這一點被髮送到軌道,我只想設置一個「機器人」控制器,它渲染模板如:

def show_robot 
    subdomain = # get subdomain, escape 
    render :text => open('robots.#{subdomain}.txt').read, :layout => false 
end 

根據您要完成的操作,您也可以使用單個模板而不是一堆不同的文件。

10

爲什麼不使用內置的看法軌道?使用robots.txt內容app/views/static_pages/robots.txt.erb

routes.rb地方:

在您的控制器添加此方法:

class StaticPagesController < ApplicationController 
    def robots 
    render :layout => false, :content_type => "text/plain", :formats => :txt 
    end 
end 

在視圖中創建一個文件

get '/robots.txt' => 'static_pages#robots' 

刪除文件/public/robots.txt

您可以根據需要添加特定的業務邏輯,但這樣我們就不會讀取任何自定義文件。

0

我喜歡TA泰裏的解決方案,但它是非常的Rails 2.x的中心所以這裏是我想出了鐵路3.1.X

mime_types.rb

Mime::Type.register "text/plain", :txt 

通過添加格式您不必擔心在控制器中使用respond_to塊。 的routes.rb

match '/robots.txt' => 'robots#robots', :format => "text" 

我加了一點東西就這一個額外的費用。搜索引擎優化人員抱怨子域和SSL頁面中的重複內容,所以我創建了兩個機器人文件,一個用於生產,另一個用於非生產,這也是生產中的任何SSL/HTTPS請求。

robots_controller.rb

class RobotsController < ApplicationController 
    def robots 
    site = request.host 
    protocol = request.protocol 
    (site.eql?("mysite.com") || site.eql?("www.mysite.com")) && protocol.eql?("http://") ? domain = "production" : domain = "nonproduction" 
    robots = File.read("#{Rails.root}/config/robots-#{domain}.txt") 
    render :text => robots, :layout => false 
    end 
end 
1

對於導軌3:

創建一個控制器RobotsController:

class RobotsController < ApplicationController 
#This controller will render the correct 'robots' view depending on your subdomain. 
    def robots 
    subdomain = request.subdomain # you should also check for emptyness 
    render "robots.#{request.subdomain}" 
    end 
end 

創建機器人次(每1子域):

  • 觀點/robots/robots.subdom ain1.txt
  • 的意見/機器人/ robots.subdomain2.txt
  • 等...

添加一個新的路線的config/routes.rb中:(注意:TXT格式選項)

match '/robots.txt' => 'robots#robots', :format => :txt 

,當然還有,你應該聲明:TXT格式的配置/初始化/ Mime_types.rb:

Mime::Type.register "text/plain", :txt 

希望它能幫助。