We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50994
    • 7 Posts
    Hi everyone.
    I have a little problem with Babel that eats out my brain for the last 3 days.
    The situation is: MODX 2.4.0, Babel 3.0.0 Beta 4. Two contexts for two languages.

    web
    base_url ——— /en/
    cultureKey ——— en
    site_start ——— 1
    site_url ——— mysite.com/en/

    russian
    base_url ——— /ru/
    cultureKey ——— ru
    site_start ——— 2
    site_url ——— mysite.com/ru/

    .htaccess contents:
    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^mysite\.com [NC]
    RewriteRule (.*) http://mysite.com/$1 [R=301,L]
    
    # The Friendly URLs part
    RewriteCond %{REQUEST_URI} (en|ru)$
    RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(ru|en)/favicon\.ico$ favicon.ico [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(ru|en)/assets/(.*)$ assets/$2 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


    Gateway plugin:
    <?php
    
    // Operating only on Front-end and only with friendly urls
    if($modx->event->name != 'OnHandleRequest' || $modx->context->key == 'mgr' || !$modx->getOption('friendly_urls'))
    {
    	return;
    }
    
    // Getting the requested url
    $alias = $modx->getOption('request_param_alias', null, 'alias', true);
    $request = &$_REQUEST[$alias];
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/log.txt', print_r($alias, true));
    
    // Selecting contexts with set base_url
    $q = $modx->newQuery('modContextSetting', array('key' => 'base_url', 'value:!=' => ''));
    $q->select('context_key,value');
    
    $contexts = array();
    $tstart = microtime(true);
    if($q->prepare() && $q->stmt->execute())
    {
    	// Accounting teh DB query in global statistics
    	$modx->queryTime += microtime(true) - $tstart;
    	$modx->executedQueries++;
    	// Fetching the result
    	while($row = $q->stmt->fetch(PDO::FETCH_ASSOC))
    	{
    		$base_url = trim($row['value'], '/');
    		$context = $row['context_key'];
    		// If the request starts with some context's base_url...
    		if(preg_match('/^(' . $base_url . ')\//i', $request))
    		{
    			// ... then we need to switch to this context
    			// Except the Web context that is being initialized in index.php
    			if($context != 'web')
    			{
    				$modx->switchContext($context);
    			}
    			// Cutting out base_url from the request so MODX could find the resource by URI
    			$request = preg_replace('/^' . $base_url . '\//', '', $request);
    			// All done - leave the loop
    			break;
    		}
    	}
    }
    


    How does it work now: http://mysite.com/en/page1/page2/... for English pages, http://mysite.com/ru/page1/page2/... for Russian ones.

    What I would be happy to get:
    http://mysite.com/page1/page2/... —> rewrite —> http://mysite.com/en/page1/page2/...
    BUT:
    http://mysite.com/ru/page1/page2/... leave un-rewritten.

    How could I solve this?

    ______________
    P.S.: And one more thing: I don't want to detect language on browser's Accept-language. The website user must be able to select language manually. And website must work in English (with /en/) by default.

    This question has been answered by culd | steffan. See the first response.

      • 37377
      • 64 Posts
      I've done it like this: (maybe not the best way)

      added the following to .htaccess
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(en|nl|fr|pt|de)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]


      and than in the gateway plugin:
      <?php
      if($modx->context->get('key') != "mgr"){
         
          $cultureKey=$_GET["cultureKey"];
          
          
          switch ($cultureKey) {
              case 'en':
                  $modx->switchContext('en');
                  break;
              case 'pt':
                  $modx->switchContext('pt');
                  break;
              case 'fr':
                  $modx->switchContext('fr');
                  break;
              case 'de':
                  $modx->switchContext('de');
                  break;
              case 'nl':
              default:
                  // by default, don't do anything
                  $modx->switchContext('web');
                  break;
          }
      }//if($modx->context->get('key') != "mgr"){
      
        • 50994
        • 7 Posts
        Your method was the first I used. But with no luck. Being set up with my other neccesary directives in .htaccess this RewriteRule caused the cyclic redirect (infinite redirection loop).

        The gateway plugin I'm using lets operate language versions without the cultureKey $_GET parameter. But how to redirect all pages without language to English and show this in address bar? sad
          • 37377
          • 64 Posts
          What happens if you just set your base_url to / and site_url to http://mysite.com/ for the English context?
            • 50994
            • 7 Posts
            When I do so it works pretty great but I need to completely separate language versions and there must be no URLs without language prefix (i.e. /[lang]/ at the beginning). .htaccess RewriteRules always been a mystery to me so I tried to solve this for two days but with no result...
              • 37377
              • 64 Posts
              Do you want it like this? http://www.proosthof.be/
              When you click on any menu item it switches to nl, but you can access the site without a language prefix as well
                • 50994
                • 7 Posts
                I can access my site without any prefixes and it would be English set as default. But the address link will show no language prefix.

                On http://www.proosthof.be/ /nl/ prefix is pre-scripted in links so it doesn't look like redirection. What I want to get is when the user goes to mysite.com the redirect to mysite.com/en/ happens but if the URL contains other language prefix the site must respond with requested language:

                mysite.com -> mysite.com/en/
                mysite.com/de -> mysite.com/de/
                mysite.com/ua -> mysite.com/ua/
                  • 37377
                  • 64 Posts
                  Ok, like this then or am I completely mistaken? smiley http://www.adyourtarget.com/

                  There are no other pages there except the customer portal, but I guess that's what you're after. That's done with my first snippet of code above btw.
                    • 50994
                    • 7 Posts
                    No, this is not what I need sad... When I enter http://www.adyourtarget.com/ the URL remains as "http://www.adyourtarget.com/" and must become "http://www.adyourtarget.com/en/" for example. Your snippet is the simplest gateway plugin I tried. The problem is in rewriting URLs on the server base level i.e. in .htaccess far before the MODX plugin subsystem starts.
                      • 37377
                      • 64 Posts
                      Ah, like this http://www.bbt4vw.com/ smiley On this website it is done with accept-language, but maybe it'll get you started. Not sure if it works correctly because it always starts in English even though my language settings are Dutch.

                      If it doesn't help then I'm sorry for wasting your time.

                      RewriteCond %{HTTP_HOST} .
                      RewriteCond %{HTTP_HOST} !^www\.bbt4vw\.com [NC]
                      RewriteRule (.*) http://www.bbt4vw.com/$1 [R=301,L]
                      
                      
                      
                      # The Friendly URLs part
                      # detect language when requesting the root (/)
                      RewriteCond %{HTTP:Accept-Language} !^en [NC]
                      RewriteRule ^$ en/ [R=301,L]
                      RewriteRule ^$ nl/ [R=301,L]
                      RewriteRule ^$ fr/ [R=301,L]
                      RewriteRule ^$ de/ [R=301,L]
                      RewriteRule ^$ es/ [R=301,L]
                      RewriteRule ^$ hr/ [R=301,L]