Add rel="nofollow" automatically to all outbound links in ASP.NET

328 views Asked by At

Any geniuses on StackOverflow ever made a solution which automatically adds rel="nofollow", to all external links?

I'd just like to apologise, I'm very new to backend coding - my attempts have literally got nowhere hence why I haven't posted them.

I've found some solutions in php, but nothing in ASP.NET.

I have a solution in jQuery, but the issue is - it'll be added after load. This is no good for telling Googlebot to ignore said links on my pages.

The jQuery solution is:

$("div.content a[href^='http']:not([href*='mysite.co.uk'])").attr("rel", 
"follow");
1

There are 1 answers

0
Aaron On

One way would be to create your own custom HttpModule that set's the response to use a derived stream class to filter the HTTP body. There is a linked example in there on how to create a basic HttpModule. Github or Nuget may have a filter class that someone has written to do modifications the output stream when it's content type is text/html that you might be able to modify for your needs.

To build one on your own you will essentially need to attach to the BeginRequest event and set a filter to the HttpApplication's response.filter. That filter will be in charge of implementation of reading the response the page/control/ihttphandler has created and modifying it before it sends it to the client and then implementing the write to the client.