Skip to content

Instantly share code, notes, and snippets.

@ojii
Created March 8, 2011 16:51
Show Gist options
  • Save ojii/860529 to your computer and use it in GitHub Desktop.
Save ojii/860529 to your computer and use it in GitHub Desktop.
import re
from django.conf import settings
from django.http import HttpResponseRedirect
DEFAULT = 'http://www.getfirefox.com'
class IE6CountdownMiddleware(object):
"""
Do *very* simple user agent scanning and redirect anyone accessing your site
using an old Internet Explorer to a website to get a real browser.
"""
def __init__(self):
self.redirect_to = getattr(settings, 'IE6_COUNTDOWN_REDIRECT', DEFAULT)
self.min_version = getattr(settings, 'IE6_COUNTDOWN_MIN_VERSION', 6)
self.regex = re.compile('MSIE (\d)')
def process_request(self, request):
user_agent = request.META.get('HTTP_USER_AGENT', '')
match = self.regex.search(user_agent)
if match and int(match.group(1)) <= self.min_version:
return HttpResponseRedirect(self.redirect_to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment