Search code examples
javascriptphphrefgoogle-searchnofollow

add nofollow attributes to my internal links


i want to apply rel="nofollow" attributes to all external links and no internal links like my function below and I am not sure how to implement it?

my second question : is it true that, There is no point in adding rel="nofollow" using JavaScript. Search engines generally don't execute JavaScript?

 <div class="post">
        <h2 class="post title" >
            <a href="https://www.exemple0.com/" title="web site" ></a>
        </h2>
    </div>
    <div class="post">
        <h3 class="post title" >
            <a href="https://www.exemple1.com/" title="web site" ></a>
        </h3>    
        <li class="tags">
          <span class="tag"><span class="tel">hello</span><br>
          <a target="_blank" href="http://www.exempleInternal.com"> internal</a>
        </li>
    </div>
    <div class="post">
        <h4 class="post title" >
            <a href="https://www.exemple2.com/" title="web site" ></a>
        </h4>
    </div>
    <script >

    $(document).ready(function() {
    (function($){
                $('a').each(function(){
                    $(this).attr('rel', "nofollow");
                });
            });
    })
    </script>

Solution

  • You can possibly check for the URL in href tag and add rel=nofollow.

    $('a[href^="http"]:not([href^="http://www.example.com"],[href^="http://example.com"])').add('a[href^="www"]:not([href^="www.example.com"])').attr('rel', "nofollow")
    

    Every link that has not 'example.com' which should be your domain name will get additional rel=nofollow.