Skip to content

Browser redirection

Herr Vigg edited this page May 31, 2020 · 3 revisions

Canonical URL

When a user comes to the site for the very first time (from an external link or by typing URL), the language is set to the one encoded in the URL used. If the URL does not have language information encoded in it, qTranslate-XT detects the language and may redirect the browser to a canonical URL for the language detected before processing a request, which is done to improve consistency and compatibility with other plugins.

The canonical URL depends on the options:

  • Default Language
  • Detect Browser Preferred Language
  • URL Modification Mode (‘query’, ‘pre-path’, ‘pre-domain’, ‘per-domain’)
  • Hide URL language information for default language

Note that options about untranslated content (to hide content or show prefix) do not affect Canonical URL.

Examples:

https://example.com/somepageslug (no language information)
https://example.com/de/somepageslug (german in pre-path mode)
https://example.com/somepageslug?lang=fr (french in query mode)

Language detection

If the URL does not have language information, then language is determined as follows, by order of priority:

  • referrer URL
  • cookie (qtrans_front_language)
  • browser setting (if main home page /)
  • default language (as set on Settings/Languages configuration page)

Once language is negotiated, a language cookie qtrans_front_language is set. The language negotiated will be kept as such during all subsequent browsing. The language is only switched if a URL has new language encoded in it. This can be achieved through language menu, widget or a custom URL with language encoded set by the web administrator/designer to force a language switch.

When option "Hide URL language information for default language" is on, Canonical URL for default language does not have language encoded. Then language will be negotiated as the default one, except when cookie is already set or referrer has different language encoded, to preserve consistency during the browsing. If referrer or cookie specify non-default language, then a redirect to the canonical URL for that language will also occur.

Cookies

Except language switching menu and widgets, all internal links on a page usually encode the active language, unless a link was created by the theme or plugin without passing it through an appropriate filter. Language cookie help to keep the same language then.

Having cookie qtrans_front_language also helps on AJAX calls, which almost never have language encoded in URL. If option ‘Hide URL language information for default language’ is on, then there is no way to detect active language correctly on AJAX call, unless the cookie is enabled.

If cookies are disabled in browser settings, then the language detection on bare URLs is done based on referrer link, which replaces cookies functionality in most of the cases, including AJAX calls. Without cookies returning users might lose their language previously chosen, but once the language is chosen again, they should be fine.

On admin side, the cookie are always expected to be enabled, as WordPress require, and the current admin language is determined by qtrans_admin_language cookie or the default language on the first visit. No redirections occur on admin side. The currently active Language Button is determined by browser session storage (no cookie).

Further reading of the original discussion at GitHub may help to those who wish to dig deeper.

Localized versions for SEO (hreflang)

When SEO robots visit your site, they will get language encoded by URL or otherwise always the default language, since robots do not use cookies or referrer. All pages also set headers <link hreflang=”xx” …> and <link hreflang=”x-default” …> as recommended by Google. This makes it consistent for SEO purpose, in spite using temporary redirect. Permanent redirect is only useful for permanent move of the pages, but for language switching, it must be temporary redirect, since all URLs with or without language encoded are valid and alive.

The nature of a common confusion is due to the dual ambiguity of a URL without language encoded, when option "Hide URL language information for default language" is on. On one hand, one may expect the URL without a language to be always resolved to the default language. On other hand, other users expect to preserve the active language during browsing, even if a URL without language encoded is used. The above is a description of how this ambiguity gets resolved.

Let's take an example where the default language on the server is fr and we use the option "hide default language".

<link hreflang="fr" href="http://example.com/fr/somepageslug" rel="alternate" />
<link hreflang="de" href="http://example.com/de/somepageslug" rel="alternate" />
<link hreflang="en" href="http://example.com/en/somepageslug" rel="alternate" />
<link hreflang="x-default" href="http://example.com/sompageslug" rel="alternate" />

The main question is about the first link. You may think the correct link is <link hreflang="fr" href="http://example.com/somepageslug" rel="alternate" /> (without language as we have checked the option to hide it).

Problem: with that link, qTranslate normally negotiates the language the first time with the browser and stores it in a cookie. Let's consider:

  • a "standard user" in France wants fr
  • a "standard user" in Germany wants de
  • a german user living in France who prefers german wants de (browser settings)
  • and so on... with all possible combinations

The result is said to be non-deterministic: the result is not known in advance because it depends on user parameters (given by the browser). So it would be wrong if the SEO assumes the default link is always fr. In our example this would only be valid for the first user but not the two others. Therefore we have to use hreflang=fr for URL fr (and this may cause a redirection for the first user). We rely instead on the hreflang=x-default for the default URL that may give different results.

However, if the "browser language detection" option is turned off, the default URL becomes deterministic for SEO purpose. In that case and if the default language is not hidden, it will appear for the first link. The link with hreflang=x-default remains the default URL so in that case you would have that same URL as a target twice.