Browse files

Add rel="nofollow" to outgoing links.

  • Loading branch information...
1 parent 5875507 commit 570edf6f4e006a57f65682499ecbc39dcb59fab8 @drx committed Jul 4, 2011
Showing with 17 additions and 0 deletions.
  1. +16 −0 main/middleware.py
  2. +1 −0 settings.py
View
16 main/middleware.py
@@ -20,6 +20,22 @@ def process_request(self, request):
return redirect(url, permanent=True)
+class NofollowMiddleware:
+ '''
+ Middleware to add rel="nofollow" to all outgoing links.
+ '''
+ def process_response(self, request, response):
+ NOFOLLOW_RE = re.compile('<a (?![^>]*rel=["\']nofollow[\'"])' \
+ '(?![^>]*href=["\']\.{0,2}/[^/])' \
+ '(?![^>]*archfinch)',
+ re.IGNORECASE)
+
+ from django.utils.encoding import smart_unicode
+
+ response.content = re.sub(NOFOLLOW_RE, u'<a rel="nofollow" ', smart_unicode(response.content, encoding='utf-8', strings_only=False, errors='strict'))
+ return response
+
+
class SearchEngineReferrerMiddleware(object):
"""
http://djangosnippets.org/snippets/1240/
View
1 settings.py
@@ -51,6 +51,7 @@
MIDDLEWARE_CLASSES = (
'django.middleware.gzip.GZipMiddleware',
+ 'archfinch.main.middleware.NofollowMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'archfinch.main.middleware.SearchEngineReferrerMiddleware',
'django.middleware.common.CommonMiddleware',

0 comments on commit 570edf6

Please sign in to comment.