Redirect between language versions with Babel

Hi there!
I have a problem after installing Babel to create EN and DE versions of my bi-lingual website. Everything works like a charm except one thing. The default index page (English) automatically redirects to its German version at mydomain.com/de/

Here is my .htacess content:

RewriteEngine On
RewriteBase /

# Prevent rewrite the .well-known directory used by LetsEncrypt by rules below of this rule
# RewriteRule "^\.well-known/" - [L]

# Prevent dot directories (hidden directories like .git) to be exposed to the public
# Except for the .well-known directory used by LetsEncrypt a.o
# RewriteRule "/\.|^\.(?!well-known/)" - [F]

# for example.com -> www.example.com use the following
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)$
RewriteRule ^(.*)$ https://www.%1/$1 [R=301,L] .

# Force rewrite to https for every host
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

# detect language when requesting the root (/)
RewriteCond %{HTTP:Accept-Language} !^en [NC]
RewriteRule ^$ en/ [R=301,L]
RewriteRule ^$ de/ [R=301,L]

# redirect all requests to /assets*
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|de)/assets(.*)$ assets$2 [L,QSA]

# redirect all other requests to cultureKey 
# to index.php and set the cultureKey parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|de)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA] 

And here is the domain (to illustrate the problem) - https://www.berlinpianopercussion.com/ - see how it is redirecting to the Offizielle Website / Ensemble Berlin PianoPercussion — what do I do wrongly?

I checked Contexts settings, etc too. Though main focus on .htaccess because the problem is this “unwanted” redirect.

It must be something silly, but i run of ideas. Please help!

The problem is probably this part of the file.
If you request the base URL (mydomain.com), the code seems to chose the language according to your browser settings (request header “Accept-Language”).

There are two rewrite rules in a row, the last one being to “de/”. Wouldn’t that always make “de/” be the language?

Also, are you using a script (using cultureKey) or plugin (ContextRouter or other) to manage which context gets loaded on the front end?

The L (Last) flag in the rule [R=301,L] tells the the rewrite engine to stop processing, when the rules gets applied. So this should be correct.

But in my opinion the ! in the condition !^en [NC] is wrong.
I believe this tells the rewrite engine to redirect to en/ if the “Accept-Language” header doesn’t start with the string en.

Thank you for your sugestions, @halftrainedharry and @snowcreative ,
Honestly I don’t speak Regex :slight_smile: I did a bit but forgot everything. The content of my .htaccess is taken from some online “how to” articles and supposed to work (example article). Also I have tried removal of this block of rules in question and it didn’t change anything.

@snowcreative I use a script from a tutorial.

<?php
/* launch the plugin on frontend with sef-url on */
if ($modx->context->key == 'mgr' || !$modx->getOption('friendly_urls') || $modx->event->name != 'OnHandleRequest') {
return;
}

/* define the current language in cultureKey */
switch ($_REQUEST['cultureKey']) {
/* switch the context */
case 'de':
$modx->switchContext('deu');
break;

/* setup default content */
default:
$modx->switchContext('web');
break;
}

/* clear out GET-parameter to ensure we don't have links like cultureKey=xy when generate urls for other components */
unset($_GET['cultureKey']);

This code is similar (though not identical) in 2 tutorials that came to my attention.

===============
Also, when you are at Offizielle Website / Ensemble Berlin PianoPercussion you can see that the US flag is linked correctly to “berlinpianopercussion.com”. However once clicked the /de/ URL opens. This is to say that the problem is not with contexts or culture keys but with the redirect.

===============
Some browsers (Mozilla, Safari) return the following error:

Error: Redirection error
An error occurred while connecting to www.berlinpianopercussion.com.
This problem can sometimes occur when cookies are disabled or rejected.

Did you clear the browser cache before testing (or use a new private window)?
I believe 301 redirects get cached in the browser.


I can’t reproduce this in Firefox.


This part from the linked article is definitively wrong:

# The Friendly URLs part
# detect language when requesting the root (/)
RewriteCond %{HTTP:Accept-Language} !^en [NC]
RewriteRule ^$ en/ [R=301,L]
RewriteRule ^$ es/ [R=301,L]
RewriteRule ^$ th/ [R=301,L]

You either have to add a different condition (RewriteCond) for every rule (RewriteRule), or at least use a different regex (^$).

OK, that’s the one I use on one site, too.

Shouldn’t the condition be:

RewriteCond %{REQUEST_URI} !^en\/ [NC]
RewriteCond %{REQUEST_URI} !^de\/ [NC]
RewriteRule ^$ en/ [R=301,L]

? The only page in question seems to be the english home page, which is the only one that doesn’t have the culture key in the URL. Wouldn’t just redirecting that one URL to “www.berlinpianopercussion.com/en/” solve the problem?

Hmm. Looks like you’ve solved the problem; the English home page shows up now. What did you do?

Yes, I am aware of the cache and test the page in the privat window and also in different browsers.

Re: Some browsers (Mozilla, Safari) return the following error: – I can’t reproduce this in Firefox.

  • Correct, I have commented out the part of url redirects that is wrong. And that fixed the “bad redirect” error. However I still can’t open .com/ without being redirected to .com/de/

@snowcreative

Hmm. Looks like you’ve solved the problem; the English home page shows up now. What did you do?
Have I? On my end I still can’t open .com/ page. It keep redirecting me to .com/de/
Also I’d prefer not to create /en/ url path. Default is default.

What is the most annoying, I built several websites with this feature and it always worked like a charm. This episode happens since I had to move to the new hosting, reinstall extras, etc… something was clearly lost in the translation :slight_smile: I checked configuration of Contexts - that seems to be correct.

Yes, everything started to work correctly once I removed

# detect language when requesting the root (/)
RewriteCond %{HTTP:Accept-Language} !^en [NC]
RewriteRule ^$ en/ [R=301,L]
RewriteRule ^$ de/ [R=301,L]

and deleted browser cache.

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