Lingua SEO Friendly URLs

About a year and a half ago sjmclean asked this question. I’m trying to achieve the same thing.

When switching languages, I get the language URLs as per following examples:

https://mydomain.com?lang=en (for English)
https://mydomain.com?lang=fr (for French)
etc …

What I am looking to achieve is to switch these to:

https://mydomain.com/en (for English)
https://mydomain.com/fr (for French)
etc …

Jimf3000 provided this response, but no specifics.

jimf3000 Reply #5, 1 year, 4 months ago

Hi sjmclean,

I've used the Lingua extra on quite a few projects and it works great.

Like aaronkent, I use .htaccess, but with the addition of Ajax.

1. By using .htaccess to allow for 'example.com/{country_code}/', you can grab the {country_code} by exploding $_SERVER['REQUEST_URI'] and checking to see if the {country_code} matches the current MODx cultureKey.
2. If they don't match, you can just do an Ajax post to example.com?lang={country_code} and refresh your page.

I’m looking for some example .htaccess code to accomplish this. Anybody have something to share?

Thanks.

I think you should go with T
T[quote=“terrybarth, post:1, topic:2336, full:true”]
About a year and a half ago sjmclean asked this question. I’m trying to achieve the same thing.

When switching languages, I get the language URLs as per following examples:

mydomain. com?lang=en (for English)
mydomain. com?lang=fr (for French)
etc …

What I am looking to achieve is to switch these to:

mydomain. com/en (for English)
mydomain. com/fr (for French)
etc …

Jimf3000 provided this response, but no specifics.

jimf3000 Reply #5, 1 year, 4 months ago

Hi sjmclean,

I've used the Lingua extra on quite a few projects and it works great.

Like aaronkent, I use .htaccess, but with the addition of Ajax.

1. By using .htaccess to allow for 'example.com/{country_code}/', you can grab the {country_code} by exploding $_SERVER['REQUEST_URI'] and checking to see if the {country_code} matches the current MODx cultureKey.
2. If they don't match, you can just do an Ajax post to example.com?lang={country_code} and refresh your page

I’m looking for some example .htaccess code to accomplish this. Anybody have something to share?

Thanks.
[/quote]

TranslatePress – Translate Multilingual sites
This is one of the best plugins for translating websites. Very simple, very good pricing plan surprised the plug-in support.

Hi, if you use LangRouter or XRouting extras you shouldn’t need to make any htaccess changes.

Hi, I’m using Lingua on my site but I’m trying to solve the friendly URL, the thing is with LangRouter or XRouting you need to create the context to get the ?lang=en to /en/ .

I don’t want to create contexts because the website was in a Evo version that used a lang plugin with TV for each field and language, currently the site has more than 500 TV’s that works perfectly so the unique inconvenience is the urls are not friendly if a language is chosen.

Maybe you can make it work, if you append the .htaccess file.
Add the following line above the existing part for friendly URLs

# The new line to convert languages in the path to a GET parameter
RewriteRule ^(en|fr|es)/(.*)$ $2?lang=$1 [QSA,DPI]

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

This line should convert a request that starts with en/ like https://mydomain.com/en/about to the usual format https://mydomain.com/about?lang=en.
Add all the languages you want to support to this list (en|fr|es) in the pattern.

This alternative line should also work:

RewriteRule ^(en|fr|es)/(.*)$ index.php?q=$2&lang=$1 [L,QSA]

To generate the URLs in the new format you probably have to change this line in the snippet lingua.selector

$language['url'] = $pageURL . (!empty($hasQuery) ? '&' : '?') . $langKey . '=' . $language[$codeField];

where the language ($language[$codeField]) is appended to the URL $pageURL.
It seems that $pageURL may or may not already contain the part for the language, so you probably have to use preg_match and preg_replace to either change the language part in $pageURL or add it.

Thank you, but I couldn’t get it working :slightly_frowning_face:

Finally I installed Babel Extra and created the contexts and pages, so according the [[++cultureKey]] is showing the TV’s that belongs to each language, I hope with this not compromise the speed and performance of the website.