Update: the following works, it prints <meta name="robots" content="noindex,follow" />
in the head section of all pages accessed via a path starting with comment
. This is to work around the fact that a page like comment/3 internally carries node/nid as arguments, as jmarkel pointed out above.
<?php
function metarobots_comment_help() {
$url_components = explode('/', request_uri());
if ($url_components[1] == 'comment') {
$elements = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'robots',
'content' => 'noindex,follow',
),
);
drupal_add_html_head($elements, 'robots');
}
}
With the aid of a helpful comment on the arg function.
Hopefully this will eventually be addressed by Meta Tags module - there's a feature request, but it's not yet clear if the module author wants to support this.
[My previous comment] I just wanted to add that using robots.txt is not the answer - as you already noticed, links keep appearing in the search results despite using Disallow: /comment
. This is expected, since robots.txt tells the bot not to crawl those pages, but it doesn't tell Google not to index it.
As explained on SEOmoz Robots.txt and Meta Robots Best Practices for Search Engine Optimization:
In most cases, meta robots with parameters "noindex, follow" should be
employed as a way to to restrict crawling or indexation.
Block with Robots.txt - This tells the engines to not crawl the given
URL but tells them that they may keep the page in the index and
display it in in results.
Block with Meta NoIndex - This tells engines they can visit but they
are not allowed to display the URL in results. (This is the
recommended method)
So the noindex meta tag you're after is indeed what you need.
robots.txt
file ?