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 <a> element, 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.

Adding a new comment

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.

Plain link

The solution

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.

Step One

Go to admin/config/content/formats/plain_text to configure the plain text format

Step Two

Tick Limit allowed HTML tags under Enabled filters

Enable limit allowed html tags

Step Three

Under Limit allowed HTML tags tab, tick Add re="nofollow" to all links.

Add rel=nofollow

Step Four

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.

Order filters correctly

Step Five

Refresh the page where you added the comment and inspect the HTML.

Link with nofollow added

The 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.

Protect yourself

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.


Conquer Drupal 8 Module Development

Don't struggle to conquer Drupal 8 module development. Action packed lessons where you will work along building your own real world modules.

20% launch discount available now!

Find out more


Comments

Hey there! I'm not sure how to write this exactly, what i've tested hasn't worked yet.

<a title="Impractical Jokers" href="http://http://www.youtube.com/watch?v=5FyUGusdIwE" target="_blank" rel="nofollow">Impractical Jokers</a>

should i put the rel="nofollow" after or before target=....??

Blair Wadman's picture

It doesn't matter, it can go before or after target.

Thanks for giving that type information.That information is so helpful to us.

I did not know how to add nofollow links to Drupal comment body and after reading this article, I have gained a lot of information regarding this concept. The screen shots were very helpful in understanding the concept and I really loved it.

Blair, this is what i need! Thank you bro.

Blair Wadman's picture

You're welcome!

Very useful Blair!!! I was looking for exactly something like this! :-)

it`s a good post , thank you

What if I wanted to make the link set to follow? I have a client who has added a listing on our site but the link is set to nofollow. In the metatags section I see an checkbox that says Follow (assumed) but checking it does nothing.

Blair Wadman's picture

Hi Mark. If the link should be set to follow, then just remove rel="nofollow". You can use rel="follow" but as far I know this is the same as not having anything. This is because the default behaviour for search engines is to follow links. So rel="nofollow" is specifically telling them not to.

Thanks for actual information!

Add new comment