2010-05-01 47 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 

希望它能帮助。