1 2 3 4 5 6 7 8 9 10 11 12 | from django.template import Library
import re
register = Library()
r_nofollow = re.compile('<a (?![^>]*nofollow)')
s_nofollow = '<a rel="nofollow" '
def nofollow(value):
return r_nofollow.sub(s_nofollow, value)
register.filter(nofollow)
|
More like this
- nofollow filter for external links by muhuk 4 years, 1 month ago
- Remove self links middleware by svetlyak 5 years, 1 month ago
- Template tag to sort a list of links by pytechd 5 years, 9 months ago
- RSS feed with content:encoded elements by philgyford 2 years, 8 months ago
- FieldsetForm by Ciantic 6 years, 1 month ago
Comments
You can use also jQuery like:
$(".comment a").attr({rel: "nofollow"});
to add rel="nofollow" attribute to all a within class .comment
#
@polarbear
This would not work. I doubt that Google interprets JavaScript to check if it may or may not contribute the current sites pagerank to a link.
#
One small issue with this is that the regex doesn't match when you just add the text "nofollow" somewhere in one of the links being tested e.g: an href with a fragment-identifier like so: "blah.com#nofollow"
The following regex prevents bypassing the filter in this way
#
building on
muffinresearch's regex:This matches only internal links. (hopefully)
#