MyBB Community Forums

Full Version: Add NoFollow to BBCode URL's
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I recently had the need to add NoFollow to the URL's on a board. The board owner used some code followed on here, but it only worked on html links and not old [URL] BBCode links...

Since I was pressed for time this seemed to be the easiest way.


Go to: AdminCP » Home » MyCode » Add New MyCode


Title:

No Follow BB-URL


Short Description:

Adds NoFollow to the BBCode URL Tags


Regular Expression:

\[url=(.*?)\](.*?)\[/url\]


Replacement:

<a href="$1" rel="nofollow">$2</a>
Hi! Im trying to stay away from using to many plugins, so my question for the author is ..does this work for mybb 1.8.5?
(2015-07-27, 03:19 PM)AmatureDJ Wrote: [ -> ]Hi! Im trying to stay away from using to many plugins, so my question for the author is ..does this work for mybb 1.8.5?

This is a bit off-topic from MYCODE but I tend to use a dirty hack of the inc/class_parser.php file (The problem is you have to note down what you've done and which file you've done it to for every time there is a core update it might switch it back to the unmodified version)

The concept is that any URL's that are placed on the site that are external to the site that it originates from automatically has nofollow added to it.

There is security concerns for using a $_SERVER value without using a mybb sanitisation process or internally set mybb class global. In this example the server domain could exist anywhere in the $fullurl string. So use this at your peril.

$nofollow is a variable that is already defined (as empty) above the lines you need to change.

You'd need to find the the lines:
$name = preg_replace("#&amp;\#([0-9]+);#si", "&#$1;", $name); // Fix & but allow unicode
$link = "<a href=\"$fullurl\" target=\"_blank\"{$nofollow}>$name</a>";
return $link;

and change it to (Notice it's just one additional IF condition to set a value to $nofollow if the servername doesn't exist somewhere in the $fullurl):

$name = preg_replace("#&amp;\#([0-9]+);#si", "&#$1;", $name); // Fix & but allow unicode
if(strpos($fullurl,$_SERVER['SERVER_NAME'])===false){ $nofollow = " rel=\"nofollow\"";}
$link = "<a href=\"$fullurl\" target=\"_blank\"{$nofollow}>$name</a>";
return $link;

I tend to use a slightly more elaborate version which I assume safer, but someones bound to point out a flaw somewhere.

Instead of doing the above version, After the lines:
$nofollow = '';
 if(!empty($this->options['nofollow_on']))
 {
 $nofollow = " rel=\"nofollow\"";
 }

Just add the following block of code:
/** nofollow haq **/
/** 
$domainmatch is the domain.tld not to add nofollow to (works with a single tld, 
split ones like domain.org.tld might be a problem since the domain would be org, 
so it might need some additional coding to work.)
**/ 
$domainmatch ='domain.tld'; 

/**   
The following uses two preg_match tests to pull the domain.tld from the fullurl 
while there is quicker, dirtier methods this one is more robust to deal with string manipulation.

The match creates $host from the domain.tld/etc? part of $fullurl
**/
preg_match('@^(?:http[s]?://)?([^/]+)@i',$fullurl, $matches);
$host = $matches[1];

// The following allows us to get just the domain.tld from the $host 
preg_match('/[^.]+\.[^.]+$/', $host, $matches);

// If our domainmatch doesn't match  the domain.tld part of $host, it's an external link so add the nofollow
if($matches[0]!==($domainmatch)){ 
$nofollow = " rel=\"nofollow\""; 
}
// Since we've $nofollow should already be initialised as blank we shouldn't need an else conditional.

If anyone spots any problems with that code It would be nice to know Wink
/** nofollow haq **/
/** 
$domainmatch is the domain.tld not to add nofollow to (works with a single tld, 
split ones like domain.org.tld might be a problem since the domain would be org, 
so it might need some additional coding to work.)
**/ 
$domainmatch ='domain.tld'; 

/**   
The following uses two preg_match tests to pull the domain.tld from the fullurl 
while there is quicker, dirtier methods this one is more robust to deal with string manipulation.

The match creates $host from the domain.tld/etc? part of $fullurl
**/
preg_match('@^(?:http[s]?://)?([^/]+)@i',$fullurl, $matches);
$host = $matches[1];

// The following allows us to get just the domain.tld from the $host 
preg_match('/[^.]+\.[^.]+$/', $host, $matches);

// If our domainmatch doesn't match  the domain.tld part of $host, it's an external link so add the nofollow
if($matches[0]!==($domainmatch)){ 
$nofollow = " rel=\"nofollow\""; 
}
// Since we've $nofollow should already be initialised as blank we shouldn't need an else conditional.

Hey thanks alot for this!
It was very easy too follow.
I copied and added the code to the very end of the class_parser.php file, its working great no errors.  Big Grin