RE: "To be safe. I would add the class name to the product description for an external link, something like "external-link"
But doesn't that means that the admin user (who writes the description and embeds the html link) has to add that class to the link?
<a
class="external-link" href=...
Then he may as well just put in the rel="nofollow" ;)
<a rel="nofollow" href=...
I.e., the op requirement is "I want to
automatically".
RE: "How to do it server-side? ...I tried all the possible code and none of them worked. "
I don't know what you tried. Unlike the JavaScript / JQuery having ability to recognize and manipulate the DOM, the
server-side just has a string / "text". There may be .NET libraries that can do it, but I think that can be a lot of overhead.
You just need a way to identify "external" links and do a string replace. I asked AI to differentiate between external and internal links, and it recommended RegEx:
string input = @"<a href=""https://www.externalone.com/"">externalone.com</a>
<a href=""http://example.com/"">Example</a>
<a href=""/internal-link"">Internal</a>
<a href=""~/another-internal-link"">Another Internal</a>";
string pattern = @"<a\s+(?![^>]*\brel=)[^>]*\bhref=""https?://[^""]*""";
string replacement = @"<a rel=""nofollow"" $0";
string result = Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase);
(I did not test this ;)