Babel + XRouting

Did anyone use Babel together with XRouting and get it to work lately?

I have a multilanguage site using contexts with URLs such as mydomain.se, mydomain.se/en/, mydomain.se/de/ etc for the various languages and it works fine. I’ve used Babel and some edits in the .htaccess file to set it up.

Now I also want to add a new context with a completely different domain, like mydomain2.se. So I’ve installed XRouting which as I understand it is supposed to work. However, upon doing this my language links stop working. I tried both with my edited .htaccess and the default one that comes with MODX.

Is XRouting not compatible with the current version of MODX and Babel, or am I doing something wrong? What does .htaccess need to look like for it to work?

MODX 2.8.2
Babel 3.0.0
XRouting 1.4.1 (I also found and tried 1.5.1)

I recently had similar issues with XRouting. I ended up having success following the very clear instructions here - skipped XRouting altogether.

Hi @robcarey, that description looks like what I did to get the languages working (/en, /de etc). However, for an additional context with its own domain I can find any instructions?

You shouldn’t have to make any changes to the default .htaccess file for XRouting to work.

Have you tried troubleshooting the problem with XRouting? When you set the system setting xrouting.allow_debug_info to Yes and append &xrouting-debug=1 to the url, then you should see what the plugin does.

What did you use before to switch the context inside MODx? A custom plugin? Is this plugin still active (and maybe interferes with XRouting)?

This is the .htaccess I’ve been using together with Babel when just having one domain and multilanguage sites in subfolders (/en, /de etc), which worked fine. I don’t think I used anything other plugins to route to the language folders but I’ll take another look just in case.

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} !^443
RewriteRule (.*) https://mydomain.se/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(sv|en|de|fr|nl)/favicon.ico$ favicon.ico [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(sv|en|de|fr|nl)/assets(.*)$ assets$2 [L,QSA]

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

When I wanted to add a new domain name I installed XRouting and changed to the default .htaccess:

RewriteEngine On
RewriteBase /

RewriteRule "^\.well-known/" - [L]
RewriteRule "/\.|^\.(?!well-known/)" - [F]

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

Then the new domain works but the subfolders don’t.

Here’s what I get when I debug XRouting:

## MODX context map:

Array
(
    [_hosts] => Array
        (
            [mydomain.se] => Array
                (
                    [0] => web
                )

            [mydomain2.se] => Array
                (
                    [0] => bhg
                )

        )

    [web] => Array
        (
            [base_url] => /
            [cultureKey] => sv
            [http_host] => mydomain.se
            [site_start] => 1
            [site_url] => https://mydomain.se/
            [wib_blog] => 38
            [wib_settings] => 3
        )

    [bhg] => Array
        (
            [base_url] => /
            [cultureKey] => sv
            [http_host] => mydomain2.se
            [site_start] => 162
            [site_url] => https://mydomain2.se/
        )

)



## Requested URL: index.php


## Requested URL with base_url: /index.php


## Matched context(s) (Array key defines match quality):

Array
(
    [1] => web
)



## Request will go to context: web


## Modified request URL: 

Seems like the language contexts aren’t found?

Ah, I actually found a gateway plugin I’d used for the routing:

<?php

if($modx->context->get('key') != "mgr"){
                /* grab the current langauge from the cultureKey request var */
                switch ($_REQUEST['cultureKey']) {
                    case 'en':
                        /* switch the context */
                        $modx->switchContext('English');
                        break;
                    case 'de':
                        /* switch the context */
                        $modx->switchContext('Deutch');
                        break;
                    case 'fr':
                        /* switch the context */
                        $modx->switchContext('Francais');
                        break;
                    case 'nl':
                        /* switch the context */
                        $modx->switchContext('Nederlands');
                        break;
                    default:
                        /* Set the default context here */
                        $modx->switchContext('web');
                        break;
                }
                /* unset GET var to avoid
                 * appending cultureKey=xy to URLs by other components */
                unset($_GET['cultureKey']);
            }

I’ve disabled it now and cleared the cache but I still get error 404 on the language folders :frowning:

Are there http_host and base_url settings for all your contexts?


Maybe instead of using XRouting you could also just change your custom plugin.
Read the value of $_SERVER['HTTP_HOST'] and then switch to the appropriate context ($modx->switchContext()) if it matches your new domain.

did you set the base_url and site_url context-setting, like here?
Creating multilingual MODx websites using Babel | Inside Creative (inside-creative.com)

Ooooooh :flushed: I actually didn’t have http_host set for the languages! Now I get both the right languages AND the correct context on the new domain :partying_face:

Some assets don’t load correctly though, and I don’t get diverted automatically to https anymore. I guess that’s a different topic but if you have some advise that would be greatly appreciated :slight_smile:

Thank you so much!

You probably have to use parts of your previous .htaccess file again.

RewriteCond %{SERVER_PORT} !^443
RewriteRule (.*) https://mydomain.se/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(sv|en|de|fr|nl)/favicon.ico$ favicon.ico [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(sv|en|de|fr|nl)/assets(.*)$ assets$2 [L,QSA]

Didn’t get that working but I found this: htaccess example for Babel · GitHub

Tried to modify it to my needs but didn’t quite get there yet. Now images load fine in the different languages but I have some other issues:

  1. The main language, Swedish, doesn’t load but goes to /en instead. I want the URL to be just mydomain.se (not /sv at the end) if possible.

  2. mydomain2.se goes to mydomain.se (because of the https redirect I guess).

    RewriteEngine On
    RewriteBase /

    RewriteRule “^.well-known/” - [L]

    RewriteRule “/.|^.(?!well-known/)” - [F]

    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{SERVER_PORT} !^443
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    #https://github.com/chsmedien/XRouting/issues/19#issuecomment-173992718
    RewriteCond %{REQUEST_URI} ^/(sv|en|de|fr|nl)$
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(sv|en|de|fr|nl)/favicon.ico$ favicon.ico [L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(sv|en|de|fr|nl)/assets(.*)$ assets$2 [L,QSA]

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

Any suggestions what to change?

I removed the “sv” bits above and now I actually think issue #1 is solved. Issue #2 remains though…

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