2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-10 18:02:57 -06:00
|
|
|
class RobotsTxtController < ApplicationController
|
|
|
|
layout false
|
2017-08-30 23:06:56 -05:00
|
|
|
skip_before_action :preload_json, :check_xhr, :redirect_to_login_if_required
|
2013-02-10 18:02:57 -06:00
|
|
|
|
2019-07-15 12:47:44 -05:00
|
|
|
OVERRIDDEN_HEADER = "# This robots.txt file has been customized at /admin/customize/robots\n"
|
|
|
|
|
2018-04-11 15:05:02 -05:00
|
|
|
# NOTE: order is important!
|
|
|
|
DISALLOWED_PATHS ||= %w{
|
2018-08-16 04:16:47 -05:00
|
|
|
/auth/
|
2018-04-11 15:05:02 -05:00
|
|
|
/assets/browser-update*.js
|
|
|
|
/email/
|
|
|
|
/session
|
|
|
|
/session/
|
|
|
|
/admin
|
|
|
|
/admin/
|
|
|
|
/user-api-key
|
|
|
|
/user-api-key/
|
|
|
|
/*?api_key*
|
|
|
|
/*?*api_key*
|
|
|
|
}
|
|
|
|
|
2013-02-10 18:02:57 -06:00
|
|
|
def index
|
2019-07-15 12:47:44 -05:00
|
|
|
if (overridden = SiteSetting.overridden_robots_txt.dup).present?
|
|
|
|
overridden.prepend(OVERRIDDEN_HEADER) if guardian.is_admin? && !is_api?
|
|
|
|
render plain: overridden
|
|
|
|
return
|
|
|
|
end
|
2018-04-16 14:43:20 -05:00
|
|
|
if SiteSetting.allow_index_in_robots_txt?
|
2019-07-15 12:47:44 -05:00
|
|
|
@robots_info = self.class.fetch_default_robots_info
|
2018-04-16 14:43:20 -05:00
|
|
|
render :index, content_type: 'text/plain'
|
|
|
|
else
|
|
|
|
render :no_index, content_type: 'text/plain'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If you are hosting Discourse in a subfolder, you will need to create your robots.txt
|
|
|
|
# in the root of your web server with the appropriate paths. This method will return
|
|
|
|
# JSON that can be used by a script to create a robots.txt that works well with your
|
|
|
|
# existing site.
|
|
|
|
def builder
|
2019-07-15 12:47:44 -05:00
|
|
|
result = self.class.fetch_default_robots_info
|
|
|
|
overridden = SiteSetting.overridden_robots_txt
|
|
|
|
result[:overridden] = overridden if overridden.present?
|
|
|
|
render json: result
|
2018-04-16 14:43:20 -05:00
|
|
|
end
|
|
|
|
|
2019-07-15 12:47:44 -05:00
|
|
|
def self.fetch_default_robots_info
|
2018-04-16 14:43:20 -05:00
|
|
|
deny_paths = DISALLOWED_PATHS.map { |p| Discourse.base_uri + p }
|
|
|
|
deny_all = [ "#{Discourse.base_uri}/" ]
|
|
|
|
|
|
|
|
result = {
|
|
|
|
header: "# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file",
|
|
|
|
agents: []
|
|
|
|
}
|
|
|
|
|
|
|
|
if SiteSetting.whitelisted_crawler_user_agents.present?
|
|
|
|
SiteSetting.whitelisted_crawler_user_agents.split('|').each do |agent|
|
|
|
|
result[:agents] << { name: agent, disallow: deny_paths }
|
|
|
|
end
|
|
|
|
|
|
|
|
result[:agents] << { name: '*', disallow: deny_all }
|
|
|
|
elsif SiteSetting.blacklisted_crawler_user_agents.present?
|
|
|
|
result[:agents] << { name: '*', disallow: deny_paths }
|
|
|
|
SiteSetting.blacklisted_crawler_user_agents.split('|').each do |agent|
|
|
|
|
result[:agents] << { name: agent, disallow: deny_all }
|
2018-03-15 16:10:45 -05:00
|
|
|
end
|
|
|
|
else
|
2018-04-16 14:43:20 -05:00
|
|
|
result[:agents] << { name: '*', disallow: deny_paths }
|
2018-03-15 16:10:45 -05:00
|
|
|
end
|
|
|
|
|
2018-04-16 14:43:20 -05:00
|
|
|
if SiteSetting.slow_down_crawler_user_agents.present?
|
|
|
|
SiteSetting.slow_down_crawler_user_agents.split('|').each do |agent|
|
|
|
|
result[:agents] << {
|
|
|
|
name: agent,
|
|
|
|
delay: SiteSetting.slow_down_crawler_rate,
|
|
|
|
disallow: deny_paths
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-02-10 18:02:57 -06:00
|
|
|
end
|
|
|
|
end
|