htaccess and multilanguage URL

I am trying to translate my URL, depending on the language selected by the user, e.g.:

www.example.com/en/discover or

www.example.com/fr/decouvrir

Obviously I want the two URL to point to the same page (where PHP handles the translation with a dictionary). I was wondering if there was a smart way in the htaccess file to do this "automatically" without having the write the rule for all the pages? For the moment I would do

RewriteRule ^en/discover$ discover.php?lang=en [NC,L]

RewriteRule ^fr/decouvrir$ discover.php?lang=fr [NC,L]

but this solution is not really the easiest if there are a lot of pages and if I want to add another language.

Thanks a lot for your help

Upvotes: 0

Views: 441

Answers (1)

Bogdan Kuštan
Bogdan Kuštan

Reputation: 5577

You should pass all arguments to single php file (dispatcher), whitch would translate route and then include needed file. Something like:

.htaccess

RewriteRule ^(.*)/(.*)$ index.php?lang=$1&include=$2 [NC,L]

PHP

// This is really bad code, just an example
$dictionary = array(
    'en' => array(
        'discover' => 'discover'
    ),
    'fr' => array(
        'decouvrir' => 'discover'
    )
);
echo $dictionary[$_GET['lang']][$_GET['include']];

Upvotes: 0

Related Questions