SiteRedirectMiddleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.contrib.sites.models import Site
from django.http import HttpResponsePermanentRedirect
from django.conf import settings

class SiteRedirectMiddleware(object):
    def process_request(self, request):
        host = request.get_host()
        site = Site.objects.get_current()

        if site.domain in host:
            return None
        return HttpResponsePermanentRedirect('%s://%s%s' % (
                request.is_secure() and 'https' or 'http',
                site.domain,
                request.get_full_path(),
        ))

More like this

  1. simple DomainsAliasMiddleware by matrix 3 years, 1 month ago
  2. PermanentRedirectMiddleware by marinho 3 years, 10 months ago
  3. URL redirects middleware by gonz 4 years, 5 months ago
  4. Redirect Multiple Domains to a Single Domain by rpoulton 4 years, 7 months ago
  5. Preferred Domain decorator function. by jamiecURLe 3 years, 6 months ago

Comments

(Forgotten your password?)