I'm trying to close all external urls with rel="nofollow" parametr:
I write this simple middleware:
import re
NOFOLLOW_RE = re.compile(u'<a (?![^>]*rel=["\']nofollow[\'"])'\
u'(?![^>]*href=["\']mysite\.com[\'"])',
re.UNICODE|re.IGNORECASE)
class NofollowLinkMiddleware(object):
def process_response(self, request, response):
if ("text" in response['Content-Type']):
response.content = re.sub(NOFOLLOW_RE, u'<a rel="nofollow" ', response.content.decode('UTF8') )
return response
else:
return response
it works, but closes all links internal and external. And I don't know how more add <noindex></noindex> tag to link.