simple DomainsAliasMiddleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.conf import settings
from django.http import HttpResponseRedirect
from django.contrib.sites.models import Site

class DomainsAliasMiddleware:
    '''
    Redirect to Site 'domain' if target domain found in DOMAINS_ALIAS
    '''
    def process_request(self, request):
        target_domain = request.META.get("HTTP_HOST", "localhost")
        if hasattr(settings, 'DOMAINS_ALIAS'):
            for domain in settings.DOMAINS_ALIAS:
                if domain == target_domain:
                    return self._redirect(request)
        return None

    def _redirect(self, request):
        if request.path == "": request.path = "/"
        newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', \
                Site.objects.get_current().domain, request.path)
        if request.GET:
            newurl += '?' + request.GET.urlencode()
        return HttpResponseRedirect(newurl)

More like this

  1. Redirect Multiple Domains to a Single Domain by rpoulton 5 years, 8 months ago
  2. Single Django App behind multiple Apache Proxies by chadmaine 6 years, 1 month ago
  3. Dynamically change admin widgets at runtime by davisd 3 years, 2 months ago
  4. PermanentRedirectMiddleware by marinho 4 years, 11 months ago
  5. Super User Conditional Page Exception Reporting by zbyte64 4 years, 10 months ago

Comments

(Forgotten your password?)