... or how to avoid duplicate content by keeping the current language in the URL
Preface: Earlier this year I posted about Django CMS 2.2 features that I want to see and one of the things mentioned there was that once you have chosen the language of the site there is no matter whether you will open "/my_page/" or "/en/my_page/" - it just shows the same content. The problem is that this can be considered both duplicate and inconsistent content.
Duplicate because you see the same content with and without the language code in the URL and inconsistent because for the same URL you can get different language versions i.e. different content.
Solution: This can be easy fixed by using a custom middleware that will redirect the URL that does not contain language code. In my case the middleware is stored in "middleware/URLMiddlewares.py"(the path is relative to my project root directory) and contains the following code.
from cms.middleware.multilingual import MultilingualURLMiddleware from django.conf import settings from django.http import HttpResponseRedirect from django.utils import translation class CustomMultilingualURLMiddleware(MultilingualURLMiddleware): def process_request(self, request): lang_path = request.path.split('/') if lang_path in settings.URLS_WITHOUT_LANGUAGE_REDIRECT: return None language = self.get_language_from_request(request) translation.activate(language) request.LANGUAGE_CODE = language if lang_path == '': return HttpResponseRedirect('/%s/' % language) if len([z for z in settings.LANGUAGES if z == lang_path]) == 0: return HttpResponseRedirect('/%s%s' % (language, request.path))
URLS_WITHOUT_LANGUAGE_REDIRECT = [ 'css', 'js', ]
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'): del request.META['HTTP_ACCEPT_LANGUAGE']
It removed the HTTP_ACCEPT_LANGUAGE header sent from the browser and Django uses the language set in its settings ad default.
URLS_WITHOUT_LANGUAGE_REDIRECT is extremely useful if you are developing using the built in dev server and serve the media files trough it. But once you put your website on production I strongly encourage you to serve these files directly by the web server instead of using Django static serve.
Final words: In Django 1.4 there will be big changes about multilingual URLs but till then you can use this code will improve your website SEO. Any ideas of improvement will be appreciated.