We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29597 ☆ A M B ☆
    • 350 Posts
    Hello all,

    Like I found out in the post here: https://forums.modx.com/thread/81919/redirect-users-to-context-based-on-browser-language-setting I needed to have a site detect the browser language and redirect to the correct context.

    Website layout is:
    English (web context) - http://www.somesite.com/en/
    German (de context) - http://www.somesite.com/de/
    French (fr context) - http://www.somesite.com/fr/
    Italian (it context) - http://www.somesite.com/it/

    The current setup is:

    • MODX Revolution 2.2.2-pl (traditional)
    • Babel2.2.5-pl

    It work great now and I am happy about it.

    But, Redirector http://rtfm.modx.com/display/ADDON/Redirector is not checking for this setting at all and I can not seem to get it to work!
    Any link will go to the English page


    I NEED REDIRECTOR TO CHECK FOR $_REQUEST[$cntxt_param] AND REDIRECT TO THE CORRECT CONTEXT

    Redirects I tried:

    promo.html > [[!++cultureKey]]/promotion.html
    Gets redirected to: http://mysite.com/en/en/promotion.html (with every language setting it goes to EN)

    promo.html > promotion.html
    Gets redirected to: http://mysite.com/en/promotion.html (with every language setting it goes to EN)

    promo.html > http://mysite.com/[[++cultureKey]]/promotion.html
    http://mysite.com/en/promotion.html (with every language setting it goes to EN)

    I have set "allow_forward_across_contexts" to yes but this does not work.

    The code:

    Redirector plugin (v1.0.3-pl)
    <?php
    /**
     * @package redirector
     */
    /* load redirector class */
    $corePath =  $modx->getOption('redirector.core_path',$scriptProperties,$modx->getOption('core_path').'components/redirector/');
    $redirector = $modx->getService('redirector','Redirector',$corePath.'model/redirector/',$scriptProperties);
    if (!($redirector instanceof Redirector)) return '';
    
    /* handle redirects */
    $search = $_SERVER['REQUEST_URI'];
    $baseUrl = $modx->getOption('base_url',null,MODX_BASE_URL);
    if (!empty($baseUrl) && $baseUrl != '/' && $baseUrl != ' ') {
        $search = str_replace($baseUrl,'',$search);
    }
    $search = ltrim($search,'/');
    $extPos = strrpos($search, '.');
    if ($extPos) {
        if ($search) {
            $redirect = $modx->getObject('modRedirect', array(
                'pattern' => $search,
                'active' => 1,
            ));
            if ($redirect) {
                $target = $redirect->get('target');
                $modx->parser->processElementTags('', $target, true, true);
                if ($target != $modx->resourceIdentifier && $target != $search) {
                    if (!strpos($target, '://')) {
                        $target = $modx->getOption('base_url').$target;
                    }
                    $modx->log(modX::LOG_LEVEL_INFO, 'Redirector plugin redirecting request for ' . $search . ' to ' . $target);
                    header('HTTP/1.1 301 Moved Permanently');
                    $modx->sendRedirect($target);
                }
            }
        }
        $search = substr($search, 0, $extPos);
    }
    if ($search) {
        $redirect = $modx->getObject('modRedirect', array('pattern' => $search, 'active' => 1));
        if ($redirect) {
            $target = $redirect->get('target');
            $modx->parser->processElementTags('', $target, true, true);
            if ($target != $modx->resourceIdentifier && $target != $search) {
                if (!strpos($target, '://')) {
                    $target = $modx->getOption('base_url').$target;
                }
                $modx->log(modX::LOG_LEVEL_INFO, 'Redirector plugin redirecting request for ' . $search . ' to ' . $target);
                header('HTTP/1.1 301 Moved Permanently');
                $modx->sendRedirect($target);
            }
        }
    }
    return;
    


    .htaccess:
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(en|fr|de|it)/favicon.png$ favicon.png [L,QSA]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(en|fr|de|it)/assets(.*)$ assets$2 [L,QSA]
    RewriteRule ^en/iphone/info6html en/iphone/info6.html [L]
    RewriteRule ^de/iphone/info.html3 de/iphone/info3.html [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(en|fr|de|it)?(\/)?(.*)$ index.php?c=$1&q=$3 [L,QSA]
    


    Gateway plugin
    <?php
    //make sure the plugin does not run on the mgr context (the manager)
      if ( $modx->context->get( 'key' ) != 'mgr') {
        /**
         * grab the current context from the request "c" parameter (or context_param_alias from system settings)
         * the alternative .htaccess friendly URL rewrite rule must be activated
         */
        $cntxt_param = $modx->getOption('context_param_alias', null, 'c');
        if ( isset( $_REQUEST[$cntxt_param] ) && $_REQUEST[$cntxt_param] != '' ) {
          switch ( $_REQUEST[$cntxt_param] ) {
                
            case 'en':
              //switch the context
              $modx->switchContext('web');
              break;
                
            case 'fr':
              $modx->switchContext('fr');
              break;
                
           case 'de':
              $modx->switchContext('de');
              break;
      
           case 'it':
              $modx->switchContext('it');
              break;
                
            default:
              switch ( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) {
            
               case 'fr':
                  $modx->switchContext('fr');
                  break;
                    
               case 'de':
                  $modx->switchContext('de');
                  break;
             
               case 'it':
                  $modx->switchContext('it');
                  break;
                    
                default:
                  $modx->switchContext('web');
                  //set the cultureKey
                  break;
              }
          }
        }
      }
    
    [ed. note: ThaClown last edited this post 13 years, 8 months ago.]
      MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
      DESIGNfromWITHIN, MPThemes and Any Screen Size
      Follow me on Twitter | Read my blog | My code on GitHub
      • 8386 ☆ A M B ☆
      • 160 Posts
      You will need to modify Redirector to take a context key, the first place Redirector will look is in the default context and first match is in default context so it will return that. Maybe extend Redirector to have a dropdown for context which can be set to a wildcard culturekey as you have incase you don't want to use a specific context.
        • 29597 ☆ A M B ☆
        • 350 Posts
        Thanks, I understand that it needs changes, but my skill are not good enough to do this... Anybody up for it?
          MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
          DESIGNfromWITHIN, MPThemes and Any Screen Size
          Follow me on Twitter | Read my blog | My code on GitHub
          • 37150
          • 2 Posts
          Je kan dit met een snippet doen. Google eens op 'languageforwarder modx'.
            • 29597 ☆ A M B ☆
            • 350 Posts
            I did goodle , thanks. Can't find what you mean got a link?
              MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
              DESIGNfromWITHIN, MPThemes and Any Screen Size
              Follow me on Twitter | Read my blog | My code on GitHub
              • 37150
              • 2 Posts
              Call the snippet in your site start page.

              <?php
              $lang = strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'],",");
                  while ($lang)
                  {
                       //check if the language is dutch
                       if (strstr($lang,"nl"))
                       {
                            //redirect the user to the dutch pages
                            header ("location: ".$modx->makeUrl(*resource id*, '', '', 'full'));
                            exit;
                        }
                        //check if the language is english
                        if (strstr($lang,"en"))
                        {
                            //redirect the user to the english pages
                            header ("location: ".$modx->makeUrl*resource id*);
                            exit;
                        }
                        //check if the language is Frans
                        if (strstr($lang,"fr"))
                        {
                            //redirect the user to the frans pages
                            header ("location: ".$modx->makeUrl(*resource id*, '', '', 'full'));
                            exit;
                        }
                        //check if the language is deens
                      
                               
                       $lang = strtok(",");
                   }
                   
                   // no defined language found, just go to the english pages
                  header ("location: ".$modx->makeUrl(*resource id*, '', '', 'full'));
                  exit;


                • 29597 ☆ A M B ☆
                • 350 Posts
                Ok cool, but to use this snippet will require a manual setting of *resource id* right?
                How can I get this to work without having to manually edit it?

                The basic redirect an multilingual part already works fine, only the Redirector link does not work like I want...
                Don't really want to add more scripts/load to the pages...
                  MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
                  DESIGNfromWITHIN, MPThemes and Any Screen Size
                  Follow me on Twitter | Read my blog | My code on GitHub
                  • 40131
                  • 40 Posts
                  Quote from: sidney at Jan 29, 2013, 08:51 AM
                  Call the snippet in your site start page.

                  <!--?php
                  $lang = strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'],",");
                      while ($lang)
                      {
                           //check if the language is dutch
                           if (strstr($lang,"nl"))
                           {
                                //redirect the user to the dutch pages
                                header ("location: ".$modx--->makeUrl(*resource id*, '', '', 'full'));
                                exit;
                            }
                            //check if the language is english
                            if (strstr($lang,"en"))
                            {
                                //redirect the user to the english pages
                                header ("location: ".$modx->makeUrl*resource id*);
                                exit;
                            }
                            //check if the language is Frans
                            if (strstr($lang,"fr"))
                            {
                                //redirect the user to the frans pages
                                header ("location: ".$modx->makeUrl(*resource id*, '', '', 'full'));
                                exit;
                            }
                            //check if the language is deens
                          
                                   
                           $lang = strtok(",");
                       }
                       
                       // no defined language found, just go to the english pages
                      header ("location: ".$modx->makeUrl(*resource id*, '', '', 'full'));
                      exit;



                  with this setup it is not possible to switch back to the default language on the home page e.g. English. That's because the snippet is placed on site_start page.
                  Any suggestion how to be reworked to switch back to the default language on the home page?
                    • 29597 ☆ A M B ☆
                    • 350 Posts
                    Thanks, I understand the snippet on the start page part, but this will not allow fot a link redirect like:

                    http://somesite.com/promotion

                    Should go to (based on lang):

                    http://somesite.com/de/promotion
                    http://somesite.com/en/promotion
                    http://somesite.com/fr/promotion
                      MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
                      DESIGNfromWITHIN, MPThemes and Any Screen Size
                      Follow me on Twitter | Read my blog | My code on GitHub