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 Amazon.com 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.
<?php 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 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 Amazon.com 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
Conclusion
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:
- How to Add Visitor Counter to Website in PHP
- How to Combine a Background Image and CSS3 Gradient on the Same Element
- How to Popup Genesis eNews Extended Opt-in Form
- How to Add Custom Logo to Genesis Child Theme
- 11 Genesis Framework Hacks for Beginner
- How to Use HTML Form [PHP]
- Starting with WordPress Metafields
- How to Store XML Data to PHP Variable
- How to Create Awesome Input Range Slider
- How to Research Competitors Using Semrush Keyword Research Tool
- How to Research Keyword Using Long Tail Pro (Step By Step Guide)
- How to Compress and Optimize Image on WordPress
- How to Add Caching Engine to WordPress Website
- How to Add Sitemaps to Google Webmaster
- How to Create Sitemaps for WordPress Website
- How to do WordPress SEO technically (OnPage Guide)
- Ahrefs vs SEMRush vs Long Tail Pro: Best Keyword Research Tool in 2020
8 thoughts on “How to Add Nofollow Tag to a Certain Domain”
Many thanks.
That saved me a whole lot of hard work.
how to add multiple domain names…for the no follow tag.
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 geni.us links
function add_nofollow( $content ) {
$az_url = “geni.us”;
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’ );
Hey, I have checked your website page https://groomandstyle.com/best-sleeping-bag-review-top-5-list
And see, “Klymit KSB 20” is linked with https://geni.us/bestsleepingbag1 and the link relation “nofollow noopener”.
I think this is working for your site.
Tried the code and broke my site.
Does it still work or does it need o be updated?
Sorry for inconvenience, I just update code. You can try it right away.
Thank you for posting this, is there anyway to apply this on multiple domains?
does not changed previous links sorry !