- July 24, 2008
- middleware redirect permanent seo
- 0 (after 0 ratings)
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.
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.
Later you must inform a setting called
HTTP_HOST_DOMAIN with the correct domain.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from django.conf import settings from django.http import HttpResponsePermanentRedirect class PermanentRedirectMiddleware(object): def process_request(self, request): if hasattr(settings, 'HTTP_HOST_DOMAIN') and\ 'HTTP_HOST' in request.META and \ request.META['HTTP_HOST'] != settings.HTTP_HOST_DOMAIN: return HttpResponsePermanentRedirect("http%s://%s%s"%( request.is_secure() and 's' or '', settings.HTTP_HOST_DOMAIN, request.get_full_path(), ) )
More like this
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 5 months ago
- Crispy Form by sourabhsinha396 5 months, 4 weeks ago
- ReadOnlySelect by mkoistinen 6 months, 1 week ago
- Verify events sent to your webhook endpoints by santos22 7 months, 1 week ago
- Django Language Middleware by agusmakmun 7 months, 2 weeks ago