Turn all links NoFollow > Is there a Script

Discussion in 'Search Engine Optimization' started by scubita, Jan 29, 2009.

  1. #1
    Guys, Guess i've been penalized on a site for too much outbound links... and i need to solve this as soon as possible to get my rankings back.

    Gotta turn NoFollow lots of links in 500 different HTML pages... I'm in trouble :eek:

    Is there any Php script or similar that turn all links NoFollow on a html site?

    Any help appreciated guys!
     
    scubita, Jan 29, 2009 IP
  2. vansterdam

    vansterdam Notable Member

    Messages:
    3,145
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    245
    #2
    You might want to get a copy of Macromedia Dreamweaver. It has a really good find/replace function that you can use on one file, all open files or even all files within a certain directory. You could first replace every occurrence of '<a href=' with '<a rel="nofollow" href='. Then do a second find/replace to remove all of the nofollows that were just added to your internal links. Something like replace '<a rel="nofollow" href="http://www.domain.com' where the link starts with your domain. If you use relative links it would be a bit more complicated.

    If that's not sufficient you might have to custom program a php script to do something similar.
     
    vansterdam, Jan 29, 2009 IP
  3. amerigohosting

    amerigohosting Peon

    Messages:
    255
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    s/<a href/<a rel="nofollow" href/
    this is a regex, if you have access to *nix then you can use this + sed to accomplish what you need....just make sure to backup your files first!

    edit:
    that should read
    
    s/<a href/<a rel="nofollow" href/g
    
    Code (markup):
     
    amerigohosting, Jan 29, 2009 IP
  4. scubita

    scubita Peon

    Messages:
    5,550
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry but i didn't understand a bit of it :eek: Is this to place in every link?
     
    scubita, Jan 29, 2009 IP
  5. amerigohosting

    amerigohosting Peon

    Messages:
    255
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sed is a linux/unix command line utility for editing files.
    you can also use gsed if it is available.
    from within a directory
    
    sed 's/<a href/<a rel="nofollow" href/g' *.html
    
    Code (markup):
    this command will substitute (that's the s in the beginning) all instances of
    
    <a href
    
    Code (markup):
    with
    
    <a rel="nofollow" href
    
    Code (markup):
    for every file that ends in .html in the directory (same as manually opening each one and doing a find/replace...only it is one one-line command per directory

    aka "adding the nofollow attribute to all links in all files in a directory"

    There is also a switch to make it recursive (all directories from within that directory) but it doesn't work with all versions of sed *ie: solaris 9
     
    amerigohosting, Jan 29, 2009 IP
  6. scubita

    scubita Peon

    Messages:
    5,550
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #6
    amerigohosting, thanks for you feedback but frankly I am lost here. How can i insert the command?

    sed 's/<a href/<a rel="nofollow" href/g' *.html
    Code (markup):
     
    scubita, Jan 30, 2009 IP
  7. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Here is a function for PHP to turn a link into no follow....

    
    function filterHref ($str) {
    $str = stripslashes($str);
    $preg = "/<[\s]*a[\s]*href=[\s]*[\"\']?([\w.-]*)[\"\']?[^>]*>(.*?)<\/a>/i";
    preg_match_all($preg, $str, $match);
    foreach ($match[1] as $key=>$val) {
    $pattern[] = '/'.preg_quote($match[0][$key],'/').'/';
    $replace[] = "<a href='$val' rel='nofollow'>{$match[2][$key]}</a>";
    }
    return preg_replace($pattern, $replace, $str);
    }
    
    PHP:
    Here is an example of implimentation...

    
    <?php
    
    	function filterHref ($str) {
    $str = stripslashes($str);
    $preg = "/<[\s]*a[\s]*href=[\s]*[\"\']?([\w.-]*)[\"\']?[^>]*>(.*?)<\/a>/i";
    preg_match_all($preg, $str, $match);
    foreach ($match[1] as $key=>$val) {
    $pattern[] = '/'.preg_quote($match[0][$key],'/').'/';
    $replace[] = "<a href='$val' rel='nofollow'>{$match[2][$key]}</a>";
    }
    return preg_replace($pattern, $replace, $str);
    }
    
    $text1 = '<a href="http://www.google.com">Google</a>';
    
    $text2 = '<a href="http://www.google.com">Google</a>';
    
    echo $text1;
    echo '<br />';
    echo filterHref($text2);
    
    
    ?>
    
    PHP:
    Ive tested it and it works fine :)
     
    !Unreal, Jan 30, 2009 IP