Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Heroku上登台服务器的不同robots.txt_Ruby On Rails_Heroku_Gitignore - Fatal编程技术网

Ruby on rails Heroku上登台服务器的不同robots.txt

Ruby on rails Heroku上登台服务器的不同robots.txt,ruby-on-rails,heroku,gitignore,Ruby On Rails,Heroku,Gitignore,我在Heroku上有登台和制作应用程序 对于crawler,我设置robots.txt文件 在那之后,我收到了来自谷歌的消息 尊敬的网站管理员,您网站的主机名, 与SSL证书中的任何“使用者名称”都不匹配,它们是: *.herokuapp.com herokuapp.com 谷歌机器人读取我的登台应用程序上的robots.txt并发送此消息。因为我没有设置任何阻止爬虫读取文件的内容 所以,我考虑的是在登台和生产之间更改.gitignore文件,但我不知道如何做到这一点 实现这一点的最佳实践是什么

我在Heroku上有登台和制作应用程序

对于crawler,我设置robots.txt文件

在那之后,我收到了来自谷歌的消息

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

谷歌机器人读取我的登台应用程序上的robots.txt并发送此消息。因为我没有设置任何阻止爬虫读取文件的内容

所以,我考虑的是在登台和生产之间更改.gitignore文件,但我不知道如何做到这一点

实现这一点的最佳实践是什么

编辑

我在谷歌上搜索了一下,找到了这篇文章

这篇文章说要设置基本的机架身份验证,您就不需要关心robots.txt

我不知道基本认证可以阻止谷歌机器人。
这个解决方案似乎更适合处理.gitignore文件。

如何使用控制器动作而不是静态文件动态提供
/robots.txt

根据您允许或不允许搜索引擎为您的应用程序编制索引的环境而定。

Rails 3的一个很好的解决方案是使用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
类中指定的任何位置)。

如果git是您想要的方式,您可以使用修改后的
.gitignore
维护一个
暂存
分支,并将该分支推送到heroku.Hmm上的暂存站点,我还是git新手,需要了解更多关于branch的信息,检查一下这个方法。谢谢。我搜索了一下,找到了一些资源,我会试试的。谢谢你的建议。事实上,文章说要将文件放在lib/robots_generator.rb中,还要将robots.txt从public/移动到config/这是一个相当古老的线程,但是有人能告诉我为什么你会在rails控制器上使用这个解决方案吗?它是更高性能还是其他什么?@dkniffin Rack如果你有一些通用的功能,你可能想从你的应用程序代码中去耦合,那就太好了。它也可以在不同的机架应用程序(如sinatra)中重用,但rails控制器也是一个很好的解决方案。我认为登台环境实际上应该使用
rails\u ENV=production
,以使其尽可能接近生产环境,而不是检查
rails.ENV.production?
,您应该向登台服务器添加一个ENV var,比如
ROBOTS=disallow
,并检查它。