Задать вопрос

Тел: +7 965 3737 888

433

Просмотров

2

Ответов

PermanentRedirectMiddleware

<p>This is a simple middleware that redirects the exactly URL requested with the correct domain. It is useful when you have more than one domain (most of cases with "www." or IP) to access a website.</p>
<p>To make it works, download the snippet file as the name "permanent_redirect.py" and add its path as the first item in MIDDLEWARE_CLASSES setting in settings.py.</p>
<p>Later you must inform a setting called HTTP_HOST_DOMAIN with the correct domain.</p>

Вопрос полезен? Да0/Нет0
file_2707.py(585.0б)
None

Ответы (2):

Ответmarinho:29.07.2008
Ответ полезен? Да0/Нет0

Fixed with mk good ideas :)

Ответmk:25.07.2008
Ответ полезен? Да0/Нет0

Here is something better for you which even works with http/https:

class ForceDomainMiddleware(object):

    def process_request(self, request):
            domain = getattr(settings, 'FORCE_DOMAIN', None)

            if not domain:
                    return

            if request.META['HTTP_HOST'] != domain:
                    target = 'http%s://%s%s' % (
                            request.is_secure() and 's' or '',
                            domain,
                            request.get_full_path())
                    return HttpResponsePermanentRedirect(target)