【问题标题】:Different robots.txt for staging server on HerokuHeroku 上用于登台服务器的不同 robots.txt
【发布时间】:2012-08-05 02:31:03
【问题描述】:

我在 Heroku 上有登台和生产应用程序。

对于爬虫,我设置了 robots.txt 文件。

之后我收到了来自 Google 的消息。

尊敬的网站管理员,您网站的主机名,https://www.myapp.com/, 与您的 SSL 证书中的任何“主题名称”都不匹配,它们是:
*.herokuapp.com
herokuapp.com

Google 机器人读取了我的临时应用程序上的 robots.txt 并发送了此消息。因为我没有设置任何东西来阻止爬虫读取文件。

所以,我正在考虑在暂存和生产之间更改 .gitignore 文件,但我不知道该怎么做。

实现这一点的最佳做法是什么?

编辑

我在谷歌上搜索并找到了这篇文章http://goo.gl/2ZHal

这篇文章说要设置基本的 Rack 身份验证,你不需要关心 robots.txt。

我不知道基本身份验证可以阻止 google bot。 似乎这个解决方案比操纵 .gitignore 文件更好。

【问题讨论】:

  • 如果 git 是您想要的方式,您可以使用修改后的 .gitignore 维护一个 staging branch,并将该分支推送到 heroku 上的暂存站点.
  • 嗯,我还是 git 新手,需要了解有关分支的更多信息,请检查该方法。谢谢。

标签: ruby-on-rails heroku gitignore


【解决方案1】:

Rails 3 的一个很好的解决方案是使用 Rack。这是一个很好的帖子,概述了这个过程:Serving Different Robots.txt Using Rack。总而言之,您将其添加到您的 routes.rb 中:

 # config/routes.rb
 require 'robots_generator' # Rails 3 does not autoload files in lib 
 match "/robots.txt" => RobotsGenerator

然后在 lib/robots_generator.rb 中创建一个新文件

# lib/robots_generator.rb
class RobotsGenerator
  # Use the config/robots.txt in production.
  # Disallow everything for all other environments.
  # http://avandamiri.com/2011/10/11/serving-different-robots-using-rack.html
  def self.call(env)
    body = if Rails.env.production?
      File.read Rails.root.join('config', 'robots.txt')
    else
      "User-agent: *\nDisallow: /"
    end

    # Heroku can cache content for free using Varnish.
    headers = { 'Cache-Control' => "public, max-age=#{1.month.seconds.to_i}" }

    [200, headers, [body]]
  rescue Errno::ENOENT
    [404, {}, ['# A robots.txt is not configured']]
  end
end

最后确保将 move robots.txt 包含到您的配置文件夹(或您在 RobotsGenerator 类中指定的任何位置)。

【讨论】:

  • 其实文章说要把文件放在 lib/robots_generator.rb 中,还要把 robots.txt 从 public/ 移到 config/
  • 这是一个相当古老的线程,但是有人能告诉我为什么你会在一个 Rails 控制器上使用这个解决方案吗?是性能更好还是什么?
  • @dkniffin Rack 如果你有一些你可能想要从你的应用程序代码中分离出来的通用功能,那就太好了。它也可以在不同的机架应用程序(如 sinatra)中重复使用,但 Rails 控制器也是很好的解决方案。
  • 我认为登台环境实际上应该使用RAILS_ENV=production 以使其尽可能接近生产环境-因此,您应该向登台服务器添加一个ENV var,而不是检查Rails.env.production?,例如@ 987654327@ 并检查它。
【解决方案2】:

使用控制器操作而不是静态文件动态地提供/robots.txt 怎么样? 根据您允许或不允许搜索引擎为您的应用程序编制索引的环境。

【讨论】:

  • 我搜索了一下,找到了几个资源,我会试试的。谢谢你的建议。
猜你喜欢
  • 2011-12-14
  • 1970-01-01
  • 2016-01-08
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多