add nofollow tag

How to Add Nofollow Tag to a Certain Domain


Do you want to add Nofollow tag to a specific domain? “Specific domain” – Yes! You probably guess the right meaning “external domain.”

I have been doing Amazon affiliate along with my web developing business. I had to focus on SEO to improve my website ranking on Google search engine. For SEO purpose, I needed to add Nofollow tag for all external links goes to Amazon.

I was thinking, how can I add Nofollow tag for domain. I didn’t want to add nofollow attribute manually again and again because there was a lot of product review content, including affiliate links of product.

Add Nofollow tag to a certain domain in WordPress

So, what I did! I always love to keep separate my code from core theme functions, file, and code. So I first create a file named nofollow.php theme file directory. Edit the file with a code editor or notepad.

Here is the code you have to copy and paste in nofollow.php. Change the value of the $az_url variable to the domain name you want nofollow tag for and save the file.


function add_nofollow( $content ) {
    $az_url = "";
    preg_match_all( '~&lt;a.*&gt;~isU', $content, $uri_match );
    for ( $i = 0; $i &lt;= sizeof( $uri_match[0] ); $i ++ ) { 

            if ( isset( $uri_match[0][ $i ] ) &amp;&amp; ! preg_match( '~nofollow~is', $uri_match[0][ $i ] ) &amp;&amp; ( preg_match( '~' . preg_quote( $az_url ) . '~', $uri_match[0][ $i ] )) ){ $uri_change = trim( $uri_match[0][ $i ], "&gt;" );
            $uri_change .= ' rel="nofollow"&gt;';
            $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );

    return $content;

// Add no-follow attribute for certain external link
add_filter( 'the_content', 'add_nofollow' );

Note: Don’t put PHP tag if you already have in the file.

We have successfully built the function to add nofollow tag to domain.

Now, we are going to call the nofollow.php file in the file of our theme's functions.php. Open functions.php with a code editor or notepad. Copy the snippet below and paste it into the functions.php file.

//* Call nofollow file
include_once( get_template_directory() . '/nofollow.php' );

Save the file, and we have done. Check the domain URL on your website either it associated with rel=nofollow or not. If you found any error don’t hesitate to comment below.


Infographic: How Nofollow tag works and effects



Usually, to maintain link juice and SEO, you’ll need to add nofollow tag for outbound links. We have a lot of outbound, affiliate, and external links to our Amazon affiliate website. I decided to add a nofollow tag for all affiliate and product links going Amazon.

Above, the snippet is the best practice to add a nofollow tag or attribute to a specific outbound domain. There are available WordPress plugins that do the same work.

We try to add the right value for the WordPress beginner in this tutorial. Let us know if the code snippet works for you.

You will love the following tutorials:


Editorial Staff

Editorial Staff

Editorial Staff is an in-house team of native WordPress developer and industry columnists lead by Anwer Ashif. Join the community of 2,77,752 users.

8 thoughts on “How to Add Nofollow Tag to a Certain Domain

  1. Hi, I tried using your code on this page but it’s not displaying properly. For example, these characters arent all valid: ‘~<a.*>~isU’,

    I tried editing it to replace the HTML characters. I made this but it still doesn’t work. Any idea why?

    // Add nofollow to links
    function add_nofollow( $content ) {

    $az_url = “”;

    preg_match_all( ‘~isU’, $content, $uri_match );
    for ( $i = 0; $i ” );
    $uri_change .= ‘ rel=”nofollow”>’;
    $content = str_replace( $uri_match[0][ $i ], $uri_change, $content );


    return $content;


    add_filter( ‘the_content’, ‘add_nofollow’ );

Leave a Reply

Your email address will not be published. Required fields are marked *