How to add rel='nofollow' to Drupal comment links
Do you get a lot of comment spam? I do. Spam prevention services like Akismet and Mollum do a great job at stopping a lot of the spam. But what happens if a user leaves a good, on topic comment and includes a link back to their site, or another site? By default, search engines like Google will treat that link as a vote towards the target site. As far as it is concerned, you added the link. This can be bad for a couple of reasons, the main one being what is known as a bad neighbourhood. If Google does not like the target site and you link to it, you can become part of its neighbourhood. You do not want that!
This is exactly what
rel='nofollow' is for. It tells search engines that you do not necessarily trust that link and they should not count it when towards the popularity of the target website. In a HTML
rel is an attribute and
nofollow is the value. It looks like this:
<a href="http://www.example.com/" rel="nofollow">Some text</a>
To start off with, let's add a link to a comment and see what happens. I have set plain text as the format for anonymous users.
When you inspect the link in the comment, you can see that Drupal has converted this to use
<a> tags. It is a clean link, so Google will think you are vouching for the target site.
Fortunately the solutions is just a few clicks away. All you need to do is configure the text format correctly. Because I am using plain text for anonymous users, that is what I will configure here. But you could do this for any text format. Here are the steps.
Go to admin/config/content/formats/plain_text to configure the plain text format
Tick Limit allowed HTML tags under Enabled filters
Under Limit allowed HTML tags tab, tick Add re="nofollow" to all links.
Under filter processing order, drag Limit allowed HTML tags so that it is after Convert URLs into links. The order of filters is very important, because Drupal will run all of them. If Limit allowed HTML tags is before Convert URLs into links, Drupal will try to add
rel='nofollow' before it has converted the URL to a link, so it will not work.
Refresh the page where you added the comment and inspect the HTML.
rel attribute with a value of
nofollow has been added. So now you are telling searching engines like Google that you do not vouch for the quality of the links, and Google should ignore them when evaluating the popularity of the target site.
More than just comments
You can use this for any user generated content, not just comments. You just need to configure the text format that the users have permission to use.
What about the homepage link?
You can allow users to add a link to their homepage when adding a comment. This will be used as a link around the users name. In the latest versions of Drupal 7,
rel='nofollow' is automatically applied to these links.
So there we have it. It is quick and easy to protect yourself against bad neighbourhoods and other SEO nastiness that comes from untrusted user generated links.
New comments for this tutorial have been turned off.