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]
<?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;
}
}
}
This question has been answered by culd | steffan. See the first response.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|nl|fr|pt|de)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]
<?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"){
http://www.adyourtarget.com/
... 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.
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.
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]