Created
March 9, 2013 01:47
-
-
Save littlepea/5122107 to your computer and use it in GitHub Desktop.
SEO fix for Django 1.4 LocaleMiddleware based on this article:
http://blog.yawd.eu/2012/impact-django-page-redirects-seo/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.core.urlresolvers import is_valid_path | |
from django.http import HttpResponsePermanentRedirect | |
from django.middleware.locale import LocaleMiddleware | |
from django.utils import translation | |
from django.utils.cache import patch_vary_headers | |
class UpdatedLocaleMiddleware(LocaleMiddleware): | |
def process_response(self, request, response): | |
language = translation.get_language() | |
if (response.status_code == 404 and | |
not translation.get_language_from_path(request.path_info) | |
and self.is_language_prefix_patterns_used()): | |
urlconf = getattr(request, 'urlconf', None) | |
language_path = '/%s%s' % (language, request.path_info) | |
if settings.APPEND_SLASH and not language_path.endswith('/'): | |
language_path = language_path + '/' | |
if is_valid_path(language_path, urlconf): | |
language_url = "%s://%s/%s%s" % ( | |
request.is_secure() and 'https' or 'http', | |
request.get_host(), language, request.get_full_path()) | |
return HttpResponsePermanentRedirect(language_url) | |
translation.deactivate() | |
patch_vary_headers(response, ('Accept-Language',)) | |
if 'Content-Language' not in response: | |
response['Content-Language'] = language | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment