Automatically Add Rel Nofollow Tags To All Affiliate Links Without A Plugin

It is really very important to add rel nofollow tags to all of your existing amazon affiliate links so that google bot will not follow all those links which are just leading to sales page of amazon. This is very important so that your site ranking will not be affected.

So, in this article you will learn how to automatically add rel nofollow tags to all those existing amazon affiliate links on your website without using any wordpress plugin.

Not only will this work with amazon.com link strings, but this method will also work wtih amzn.to or amzn.com and any kind of affiliate links you have in your wordpress site.

If this method is implemented, it will add nofollow tags on, not just one affiliate links, but all amazon affiliate links including its shortened versions.

The good thing about this method is that, since you are not using any plugin, there is no additional consumption of your server resources.

Before I proceed, let me tell you this, there are so many wordpress plugins available in the wordpress repository. What I mean there are so many nofollow plugins available there.

Some or many of them are good. They can make you add rel nofollow tags on all of your external links: And let me share some of the good ones I used before that I no longer use now because of the disadvantages they all have.

WordPress Plugins With External Nofollow Functionality

So Here below are the good nofollow wordpress plugins I used before:

Pros of Using External nofollow Plugins

The good thing about these plugins I mentioned above is that, they are capable of putting a nofollow tags to all of your external links globally. This only means all of your affiliate links leading to external site will be added automatically with re follow attributes.

Cons of Using External nofollow Plugins

But the problem is that, if you are going to enable the functionality of these plugins, they will add rel nofollow tags on all of the external links found on your wordpress site. And that includes those good links leading to informative sites which can help you rank well in SEO.

To be specific, all the external and helpful outside links like we wikipedia, and some similar sites with links which you intentionally put on our website to help readers find even more useful information will be integrated with rel nofollow tags.

Those sites can help your SEO ranking and at the same time can help your website readers or visitors to find and read even more information, but the plugins I mentioned above will affect those links by adding rel nofollow on them all.

So here is a free solution:

Rel Nofollow Script – Can Add Rel Nofollow Tags on Certain External Links

The script I have provided below will automatically add rel nofollow tags on certain domain strings you want to be added with nofollow attributes. This means it is not only limited to amazon, but you can also use this on all of your affiliate links strings.

Where To Place This Rel Nofollow Script?

You only need to add the below scripts at the last part of your theme functions, you can find in your website’s server. You can also directly go to your theme editor on your wordpress dashboard under appearance and locate the Theme functions and place the below script at the bottom part of it.

The good thing about this is that, you only need to add the main domain of your affiliate links. Like for example: amazon.com, or go.magik.ly.

You only need to put the main domain of the affiliate links and all of the link strings coming from those domains will be added with rel nofollow tags including the main domain.

So far, I have found no wordpress plugins available in the wordpress repository that can do this feature:

So here below are the nofollow script you can use and implement right away on your affiliate sites

1. Script to Add Rel Nofollow To A Single Affiliate Domain

This is for single URL strings:

In my example below, I used amazon.com. So, you need to replace it with your own affiliate link. And there is no need for you to add http, https, and https://www. But just add the main domain name with dot com like the example below.

Again, let me remind you as I already mentioned above. If your affiliate link is https://www.amazon.com/afflink-strings-richard1234, then you only need to add amazon.com like the example below:

// Add no-follow tags for specific domain strings
function add_nofollow( $content ) {

    $az_url = "amazon.com";
    preg_match_all( '~<a.*>~isU', $content, $uri_match );

    for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
        if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
             && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
            ) {
            $uri_change = trim( $uri_match[0][ $i ], ">" );
            $uri_change .= ' rel="nofollow">';
            $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
        }
    }

    return $content;
}

add_filter( 'the_content', 'add_nofollow' );

2. Script to Add Rel Nofollow To More Than One Affiliate Domains

The script below will add rel nofollow on multiple affiliate links. In my example below, I use amazon1.com, amazon2.com and amazon3.com.

So, if your affiliate links are a https://www.amazon1.com/ref-joe1234, you only need to put the main domain of the affiliate link which is amazon1.com. And do the same with the rest of the affiliate links you want to be added with nofollow attributes.

// Add no-follow tags for specific domain strings
function add_nofollow( $content ) {

    $az_url = "amazon1.com";
    preg_match_all( '~<a.*>~isU', $content, $uri_match );

    for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
        if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
             && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
            ) {
            $uri_change = trim( $uri_match[0][ $i ], ">" );
            $uri_change .= ' rel="nofollow">';
            $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
        }
    }

    $az_url = "amazon2.com";
    preg_match_all( '~<a.*>~isU', $content, $uri_match );

    for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
        if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
             && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
            ) {
            $uri_change = trim( $uri_match[0][ $i ], ">" );
            $uri_change .= ' rel="nofollow">';
            $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
        }
    }

    $az_url = "amazon3.com";
    preg_match_all( '~<a.*>~isU', $content, $uri_match );

    for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
        if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
             && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
            ) {
            $uri_change = trim( $uri_match[0][ $i ], ">" );
            $uri_change .= ' rel="nofollow">';
            $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
        }
    }

    return $content;
}

add_filter( 'the_content', 'add_nofollow' );

In the example above, the number of domains that can be added with rel nofollow tags are 3 domains. But you can make it 4 or even more affiliate domains.

So, if you have four or more affiliate domains, just duplicate the script in the middle. Here below is the script in the middle you need to duplicate.

    $az_url = "amazon.com";
    preg_match_all( '~<a.*>~isU', $content, $uri_match );

    for ( $i = 0; $i <= sizeof( $uri_match[0] ); $i ++ ) {
        if ( isset( $uri_match[0][ $i ] ) && ! preg_match( '~nofollow~is', $uri_match[0][ $i ] )
             && ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] ))
            ) {
            $uri_change = trim( $uri_match[0][ $i ], ">" );
            $uri_change .= ' rel="nofollow">';
            $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );
        }
    }

Make sure that you duplicate the right code in the script. You can definitely understand this by just comparing the script for the single domain and the script for the multiple domains.

Additional Info About Adding Rel Noffolow Script.

If you have successfully implemented the script above in your websites, what will happen is:

  • amazon.com will be added with nofollow tags
  • amazon.com/ref-joewills-20 will be added also with nofollow tags.
  • https://www.amazon.com will have nofollow tags on it.
  • https://www.amazon.com/your-affiliate-link-strings will also be added with nofollow attributes.

So, that’s all about adding rel nofollow tags on specific domains and domain strings. After you implement this method, you can inspect the page source of your website using any web browser of your choice. And you will see that there are rel nofollow tags on all of the links you implemented with this method.

If you found this article helpful, please share this with your friends or others in your social media.

Leave a Reply

17 − seventeen =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WPreformer uses affiliate links. This means if you click and make a purchase through those links on this site, WPreformer will receive a small commision. However, purchasing products or services via affiliate links doesn't cost any extra. Click Here For Full Disclosure.
Don`t copy text!