不同的robots.txt对Heroku的临时服务器(Different robots.txt fo

2019-06-26 18:35发布

我在Heroku分期和生产应用。

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

从那以后,我从谷歌得到了消息。

亲爱的站长,您的网站的主机名, https://www.myapp.com/ ,不符合任何您的SSL证书,这是“主题名称”中:
* .herokuapp.com
herokuapp.com

在谷歌机器人阅读我的升级应用程序中的robots.txt和发送此消息。 因为我没有设置防止爬虫读取文件什么。

所以,我在想什么的是改变分期和生产之间的.gitignore文件,但我无法弄清楚如何做到这一点。

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

编辑

我GOOGLE了这件事,发现这篇文章http://goo.gl/2ZHal

这篇文章说,设置基本机架认证,你会不会需要关心的robots.txt。

我不知道基本的认证,可以防止谷歌机器人。 看来这个解决方案是更好的操纵的.gitignore文件。

Answer 1:

什么服务/robots.txt动态使用一个控制器的动作,而不是对具有静态的文件? 根据环境的不同,你允许或禁止搜索引擎索引你的应用程序。



Answer 2:

使用Rails 3伟大的解决方案是使用机架。 下面是概括的过程中伟大的职位: 服务提供不同的robots.txt使用机架 。 总之,您添加到您的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

最后,确保包括移动的robots.txt到你的config文件夹(或任何你在你指定RobotsGenerator类)。



文章来源: Different robots.txt for staging server on Heroku