Jump to content

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


Michkael
 Share

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

 

Link to comment
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...

Link to comment
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)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...