0

I want to add rel="nofollow" attribute in all my external links. Below code works well on links comes with http:// and https://. my problem is its not worked on links without http (ex: /somepage.html) how can i over come this.

Any solution highly appreciated.

JS Fiddle

HTML

<a href="http://www.somepage.com">This is an internal link</a>
<br /><br />
<a href="http://google.com">And this is an external link, with no follow (working)</a>
<br />
<br />
<a href="google.com">And this is an external link, with no follow (not-working)</a>
<br />
<br />
<a href="https://plus.google.com/101147114025225446015/about">And this is an external link, with no follow (working)</a>

JS

jQuery('a[href*="http://"]:not([href*="http://www.somepage.com"])').attr('rel', 'nofollow');

jQuery('a[href*="https://"]:not([href*="http://www.somepage.com"])').attr('rel', 'nofollow');

jQuery('a[href*="https://"]:not([href*="https://www.somepage.com"])').attr("target", "_blank");

JS Fiddle

Gold Pearl
  • 1,922
  • 3
  • 17
  • 28
  • why not verify link with current window.location. if same domain (disregard http/https) then ignore, else add rel=nofollow – Ji_in_coding Jan 23 '15 at 13:08
  • 1
    `Link` is not an external link. It effectively is a link to `/google.com`. – techfoobar Jan 23 '15 at 13:09
  • @Ji_in_coding can you give me any examples? – Gold Pearl Jan 23 '15 at 13:09
  • @techfoobar you are right. can you see my fiddle i didn't get exact answers. – Gold Pearl Jan 23 '15 at 13:12
  • I just noticed what techfoodbar has responded. He has a point there. google.com will not take you to www.google.com. So what would be the expected outcome of the case: href="google.com" ? – Ji_in_coding Jan 23 '15 at 13:13
  • @GoldPearl - Your current code is correct. You might want to add the case where you account for links starting in `//` as they can be external links (depending on the domain) - For example: `Link` is an external link. – techfoobar Jan 23 '15 at 13:37
  • Are you concern about losing Page Rank from your HTML page, caused links to external sites? If yes, I suggest you rewrite your HTML LINK code at server side, as any jQuery DOM validation done at the client won't count for any Search Engine crawler. – GibboK Jul 10 '15 at 14:18

0 Answers0