Jump to content
Sign in to follow this  
Michkael

[problem] HTTP automatic redirect to user language - redirect loop

Recommended Posts

Good day,

I have recently added a second language to my website and damn... processwire and its module are priceless!
When I tried to automatically redirect the people to their set-up browser language, I encountered a problem with the metadata on linkedin. 
The error on linkedin was saying that the page was redirected in loop and hence they were not posting the metadata.
 

The following code was right after the </header> in my _head.inc:

<?php if(!$session->get('setLang')) {
if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) != 'en') {
    $session->set('setLang','2');
    $url = $page->localUrl('Italiano');
        $session->redirect($url);
}
    $session->set('setLang','1');
} ?>

When I remove it, it works fine.
Do you have any idea on how to obtain the same result, avoiding redirect loops problems?

Thank you very much.

Michael

 

Share this post


Link to post
Share on other sites

Well, if you see your customer uses a french (or any other country except english) browser, you detect its not english and redirect to italian. Since the browser does not change, you again detect its not english and redirect...

Share this post


Link to post
Share on other sites
if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) != 'en') {

This line does not care about the region? Just about the set language, right?
If en is the default language, should I somehow change the code?

Share this post


Link to post
Share on other sites

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language

That field is about the language "the client is able to understand, and which locale variant is preferred". It has nothing to to with the reqion.

15 hours ago, Michkael said:

If en is the default language, should I somehow change the code?

At least the code should not redetect depending on a decision which does not depend on the redirection.

So code is probably something like:

if preferredLanguage is not sessionLanguage and installedLanguages(preferredLanguage) exists then redirectSession(preferredLanguage)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...