rails中子域的多个robots.txt [英] Multiple robots.txt for subdomains in rails

查看:42
本文介绍了rails中子域的多个robots.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个子域的站点,我希望命名的子域 robots.txt 与 www 的子域不同.

I have a site with multiple subdomains and I want the named subdomains robots.txt to be different from the www one.

我尝试使用 .htaccess,但 FastCGI 不查看它.

I tried to use .htaccess, but the FastCGI doesn't look at it.

所以,我试图设置路由,但似乎您不能直接重写,因为每个路由都需要一个控制器:

So, I was trying to set up routes, but it doesn't seem that you can't do a direct rewrite since every routes needs a controller:

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

解决此问题的最佳方法是什么?

What would be the best way to approach this problem?

(我正在为子域使用 request_routing 插件)

(I am using the request_routing plugin for subdomains)

推荐答案

实际上,您可能希望在 mime_types.rb 中设置 mime 类型并在 respond_to 中进行> 阻止它不会以 'text/html' 的形式返回:

Actually, you probably want to set a mime type in mime_types.rb and do it in a respond_to block so it doesn't return it as 'text/html':

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

然后,您的路线将如下所示:

Then, your routes would look like this:

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

对于 rails3:

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

和控制器类似的东西(把文件放在你喜欢的地方):

and the controller something like this (put the file(s) where ever you like):

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

冒着过度设计的风险,我什至可能会尝试缓存文件读取操作...

at the risk of overengineering it, I might even be tempted to cache the file read operation...

哦,是的,您几乎肯定需要删除/移动现有的 'public/robots.txt' 文件.

Oh, yeah, you'll almost certainly have to remove/move the existing 'public/robots.txt' file.

精明的读者会注意到,您可以轻松地将 RAILS_ENV 替换为 subdomain...

Astute readers will notice that you can easily substitute RAILS_ENV for subdomain...

这篇关于rails中子域的多个robots.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆