Multiple robots.txt for subdomains in rails

后端 未结 6 2093
轻奢々
轻奢々 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:48

    For Rails 3:

    Create a controller RobotsController:

    class RobotsController < ApplicationController
    #This controller will render the correct 'robots' view depending on your subdomain.
      def robots
        subdomain = request.subdomain # you should also check for emptyness
        render "robots.#{request.subdomain}"
      end
    end
    

    Create robots views (1 per subdomain):

    • views/robots/robots.subdomain1.txt
    • views/robots/robots.subdomain2.txt
    • etc...

    Add a new route in config/routes.rb: (note the :txt format option)

    match '/robots.txt' => 'robots#robots', :format => :txt
    

    And of course, you should declare the :txt format in config/initializers/Mime_types.rb:

    Mime::Type.register "text/plain", :txt
    

    Hope it helps.

提交回复
热议问题