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.]