benzkji
benzkji

Reputation: 1867

multilingual django cms site: seo friendlier home pages (redirects)

NOTE / EDIT: As I've learnt, the default behaviour is ok SEO wise (one redirect is allowed...multiple is no good). So this is a bit of an overkill.

If my django-cms site is multilingual, visiting domain.com always redirects to domain.com/default-language/.

Is there a preferred way/package to make this behaviour go away?

Reason I want it are mainly because of SEO. Best solution would be:
domain.com => no redirect
domain.com/default-lang/ => redirect back to domain.com
domain.com/other-lang/ => stay as is, as there is translated content

Example: http://www.parkhotel-bellevue.ch/ redirects to http://www.parkhotel-bellevue.ch/de/, which is what I dont want. http://www.parkhotel-bellevue.ch does it correctly, now.

NOTE: this question is about django-cms, not django alone.

Upvotes: 3

Views: 659

Answers (1)

Agate
Agate

Reputation: 3242

What if you put your Index url in root conf, and all your other pages under i18n_patterns ?

urlpatterns = [
    url(r'^$', Index.as_view(), name='index'),
]

urlpatterns += i18n_patterns('',
    url(r'^', include('cms.urls')),
)

This way, your root URL won't redirect to language specific URL.

For the second part of your question, you could try the following solutions:

  • If you have a limited, fixed set of languages, you can hardcode the redirects on your webserver conf (or in your django urls).
  • If you don't want to hardcode these redirects, maybe including your Index view in your i18n_patterns as well could do the trick

Something like:

# views.py
class Index(View):
    def dispatch(self, request, *args, **kwargs):
        if request.path != '/':
            return redirect('/')
        return super().dispatch(request, *args, **kwargs)

# urls.py
urlpatterns = [
    url(r'^$', Index.as_view(), name='index'),
]

urlpatterns += i18n_patterns('',
    url(r'^$', Index.as_view(), name='index'),
    url(r'^', include('cms.urls')),
)

EDIT:

Another option could be to use your own LocaleMiddleware by subclassing the one from django. The redirection part seems to happen here: https://github.com/django/django/blob/master/django/middleware/locale.py#L29

Upvotes: 1

Related Questions