def lookup(self):
        """
        The meat of this middleware.

        Returns None and sets settings.SITE_ID if able to find a Site
        object by domain and its subdomain is valid.

        Returns an HttpResponsePermanentRedirect to the Site's default
        subdomain if a site is found but the requested subdomain
        is not supported, or if domain_unsplit is defined in
        settings.HOSTNAME_REDIRECTS

        Otherwise, returns False.
        """

        # check to see if this hostname is actually a env hostname
        if self.domain:
            if self.subdomain:
                self.domain_unsplit = '%s.%s' % (self.subdomain, self.domain)
            else:
                self.domain_unsplit = self.domain
            self.domain_requested = self.domain_unsplit

        # check cache
        cache_key = 'site_id:%s' % self.domain_unsplit
        site_id = cache.get(cache_key)
        if site_id:
            SITE_ID.value = site_id
            try:
                self.site = Site.objects.get(id=site_id)
            except Site.DoesNotExist:
                # This might happen if the Site object was deleted from the
                # database after it was cached.  Remove from cache and act
                # as if the cache lookup failed.
                cache.delete(cache_key)
            else:
                return None

        # check database
        try:
            self.site = Site.objects.get(domain=self.domain)
        except Site.DoesNotExist:
            return False
        if not self.site:
            return False

        SITE_ID.value = self.site.pk
        cache.set(cache_key, SITE_ID.value, 5*60)
        return None
The meat of this middleware.

        Returns None and sets settings.SITE_ID if able to find a Site
        object by domain and its subdomain is valid.

        Returns an HttpResponsePermanentRedirect to the Site's default
        subdomain if a site is found but the requested subdomain
        is not supported, or if domain_unsplit is defined in
        settings.HOSTNAME_REDIRECTS

        Otherwise, returns False.