Multiple robots.txt for subdomains in rails

后端 未结 6 2159
轻奢々
轻奢々 2021-01-31 23:21

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

I tried to use .htaccess, but the FastCGI doesn\'t lo

6条回答
  •  野性不改
    2021-01-31 23:43

    As of Rails 6.0 this has been greatly simplified.

    By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the .text.erb extension for the layout file. Source

    class RobotsController < ApplicationController 
      def robots
        subdomain = request.subdomain # Whatever logic you need
        robots = File.read( "#{Rails.root}/config/robots.#{subdomain}.txt")
        render plain: robots
      end
    end
    

    In routes.rb

    get '/robots.txt', to: 'robots#robots'
    

提交回复
热议问题