3

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.

benzkji
  • 1,718
  • 20
  • 41
  • if you want to remove the multilingual identifiers in the url and want to use it only single langauge refer [this](http://stackoverflow.com/questions/17959941/how-to-remove-the-language-identifier-from-django-cms-2-4-urls) – Allen Fernandes Nov 17 '15 at 09:50
  • my site is multilingual. it only concerns the home page, ie http://domain.com/ without any more slug/address parts.. – benzkji Nov 17 '15 at 09:53

1 Answers1

1

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

Agate
  • 3,152
  • 1
  • 19
  • 30
  • I can either put cms.urls in i18n_patterns or not: http://docs.django-cms.org/en/latest/how_to/install.html#url-configuration . what I wonder: Is the redirect django made, or is it a "feature" of django-cms? – benzkji Nov 17 '15 at 10:08
  • I think the redirect is django-made. The behaviour seems to be: when a URL is requested and is not prefixed with a language code, we redirect to the same URL with a language code. – Agate Nov 17 '15 at 10:14
  • thx! subclassing djangos LocaleMiddleware must be it. Will let you know when it works! – benzkji Nov 17 '15 at 12:25
  • 1
    after subclassing LocaleMiddleware and handling redirects there, there were still some gotchas. for example rendering the home page with default language, using the standard cms details view. but was easy ;-) your answer did not fully answer my question, but definitely pushed me in the right direction! – benzkji Nov 24 '15 at 08:06