Alias - htaccess

Hello all,

my website is multilingual and runs with Babel. I have also created a gateway plugin for this, set the contexts and made entries in htaccess as it says in the tutorial.

The problem is: as soon as I use e.g. the letters “fr” > “fragen.html” in the alias, my content switches to “french”.

Unfortunately I can’t find the source of the error.

MODx 2.8.3
PHP: 7.3

gateway:

<?php
if($modx->context->get('key') != "mgr"){
switch ($_REQUEST['cultureKey']) {
    default:
        $modx->switchContext('web');
        break;
    case 'en':
        $modx->switchContext('en');
        break;  
    case 'fr':
        $modx->switchContext('fr');
        break; 
    case 'sp':
        $modx->switchContext('sp');
        break; 
}
unset($_GET['cultureKey']);
}

htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|sp|fr|de)/favicon.ico$ favicon.ico [L,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|sp|fr|de)/assets(.*)$ assets$2 [L,QSA]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|sp|fr|de)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]

The problem is, that in this rule the slash in the url is optional: /?
When you request the url “fragen.html”, that gets converted to index.php?cultureKey=fr&q=agen.html.

You probably need different rules to make it work. Maybe something like this:

...
#for www.mydomain.com/en or www.mydomain.com/sp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|sp|fr|de)$ index.php?cultureKey=$1 [L,QSA]

#for www.mydomain.com/en/ or www.mydomain.com/en/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|sp|fr|de)/(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]

#for everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
1 Like

Thanks for the solution! That seems to work so far.

The rule RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] may create a PHP warning in the gateway plugin ($_REQUEST['cultureKey']), because the parameter cultureKey is not set.
Maybe you have to adjust the plugin code or change the rule to

RewriteRule ^(.*)$ index.php?q=$1&cultureKey= [L,QSA]

And maybe add a empty q parameter to the first rule to be consistent.

RewriteRule ^(en|sp|fr|de)$ index.php?cultureKey=$1&q= [L,QSA]

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.