This code will add a nofollow attribute to external pages on your website

I found out that Spammers has turned one my website into their hub. So, I came up with this code to discourage them from posting on my site and also remain on the good side of Google. The code will automatically attribute a rel=”nofollow” to all the external anchor tags on your website.

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
$(document).ready(function(){ $("a[href^='http://']").each(
	function(){
		if(this.href.indexOf(location.hostname) == -1) {
			$(this).attr('rel', 'nofollow');
		}
	});
	$("a[href^='https://']").each( function(){
		if(this.href.indexOf(location.hostname) == -1) {
			$(this).attr('rel', 'nofollow');
		}
	});
});
</script>

Simply paste the code on the <head></head> section of your website.

Leave a Reply