Add rel="nofollow" to outgoing links only

1.8k views Asked by At

Please help I am using MYBB forum and I want to add rel="nofollow" to all the outgoing links but not internal.

This code works but it ads nofollow to all the links how to make it check if the link is internal and if not add nofollow

function nofollowlink_changelink($message){

        $search = '<a';
        $replace = '<a rel="nofollow"';
     $message = str_replace($search  , $replace  , $message);
     return $message;
        }

I wan't this code not to add nofollow to $mybb->settings['bburl']

Also here you have some more code to play with.

function nofollowexternal_changelink($message) {
    global $mybb;
    $message = preg_replace_callback("/<a href=\"(.+)\" (.+)>(.+)<\\/a>/",  "replace_external", $message);
    return $message;
}
function replace_external($groups) {
    global $mybb;
    $url = str_replace(array(".", "/"), array("\\.", "\\/"), $mybb->settings['bburl']);
    $urls = preg_replace("/^http/","https", $url, 1);
    if (preg_match("/$url/", $groups[1]) || preg_match("/$urls/", $groups[1])) {
        return $groups[0];
    }
    else {
        return "<a href=\"".$groups[1]."\" target=\"_blank\" rel=\"nofollow\">".$groups[3]."</a>";
    }
}

This code doesn't work if URLs are post in this format: URL, URL, URL, URL it only adds no follow to the last one. Please help

0

There are 0 answers