Prevent GoogleBot Overload with Default Nofollow

Comments

Here’s a quick tip to exert greater control over which parts of your site a search engine should crawl: modify your link_to helper to make links rel=”nofollow” by default. It’s easy:

The Code

Just add this to your app’s application_helper:

      def link_to(*args, █)
        unless block_given?
          html_options = args[2] || {}
    
          unless html_options.delete(:follow)
            if html_options[:rel]
              html_options[:rel] += ' nofollow'
            else
              html_options[:rel] = 'nofollow'
            end
          end
    
          args[2] = html_options
        end
    
        super(*args)
      end

Use WillPaginate? You may also want to add the following (adapted from this StackOverflow post):

      def will_paginate(*args)
        options = args.extract_options!
        options[:renderer] = PaginationNoFollow unless options[:renderer] || options.delete(:follow)
        super(args.first, options)
      end

Set PaginationNoFollow up with an initializer:

    require 'will_paginate/view_helpers/link_renderer'
    
    class PaginationNoFollow < WillPaginate::ViewHelpers::LinkRenderer
      def rel_value(page)
        case page
        when @collection.previous_page; 'prev nofollow' + (page == 1 ? ' start nofollow' : '')
        when @collection.next_page; 'next nofollow'
        when 1; 'start nofollow'
        else
          'nofollow'
        end
      end
    end

Now, all of your app’s links will default to rel=”nofollow”, but obviously you will want some of your links to be crawled. Just add :follow => true to the options hash of either link_to or will_paginate to opt-in specific links for search engine crawling.

comments powered by Disqus