Skip to main content
Add rel="nofollow" to "Reply" links in Drupal 7 Comments

Another SEO issue in Drupal that can waste a crawling budget is the "Reply" links in comments. By default, of course, they are closed, even in Drupal's standard robots.txt, but I've found that's not enough, as spiders steal them anyway, and then add them to the Google search console as excluded because of robots.txt

pokrytie.png

It makes more sense to add a "nofollow" attribute to these links

In the function comment_links($comment, $node), in lines 1055 and 1078 of /modules/comment/comment.module.php, add the following code:

 'attributes' => array('title' => t('reply'), 'rel' => 'nofollow'),

As a result, the type of function should be as follows:

function comment_links($comment, $node) {
  $links = array();
  if ($node->comment == COMMENT_NODE_OPEN) {
    if (user_access('administer comments') && user_access('post comments')) {
      $links['comment-delete'] = array(
        'title' => t('delete'),
        'href' => "comment/$comment->cid/delete",
        'html' => TRUE,
      );
      $links['comment-edit'] = array(
        'title' => t('edit'),
        'href' => "comment/$comment->cid/edit",
        'html' => TRUE,
      );
      $links['comment-reply'] = array(
        'title' => t('reply'),
        'href' => "comment/reply/$comment->nid/$comment->cid", 
        'html' => TRUE,
		 'attributes' => array('title' => t('reply'), 'rel' => 'nofollow'),
      );
      if ($comment->status == COMMENT_NOT_PUBLISHED) {
        $links['comment-approve'] = array(
          'title' => t('approve'),
          'href' => "comment/$comment->cid/approve",
          'html' => TRUE,
          'query' => array('token' => drupal_get_token("comment/$comment->cid/approve")),
        );
      }
    }
    elseif (user_access('post comments')) {
      if (comment_access('edit', $comment)) {
        $links['comment-edit'] = array(
          'title' => t('edit'),
          'href' => "comment/$comment->cid/edit",
          'html' => TRUE,
        );
      }
      $links['comment-reply'] = array(
        'title' => t('reply'),
        'href' => "comment/reply/$comment->nid/$comment->cid", 
		 'html' => TRUE,
		 'attributes' => array('title' => t('reply'), 'rel' => 'nofollow'),
       
      );
    }
    else {
      $links['comment_forbidden']['title'] = theme('comment_post_forbidden', array('node' => $node));
      $links['comment_forbidden']['html'] = TRUE;
    }
  }
  return $links;
}

That's it, now the new reply links will not join the ranks of the excluded in Search console