.htaccess rule for language detection

.htaccess rule for language detection

i’m stuck with some problem with .htaccess
my simplified file server structure is the following:
/index.php
/signup.php

i need to work with virtual urls for handling the language switch:
www.mydomain.com/en/signup.php
www.mydomain.com/de/signup.php

so my .htaccess should check if url contains /en/ or /de/ and translate it to eg. /signup.php?language=de
basically spoken, it should strip the language tag but keep the rest of the folder structure.
it should work on both my local xampp and on my live server.
any ideas? thanks

Solutions/Answers:

Solution 1:

What have you tried so far? Something like the following should work:

RewriteRule ^/(en|de)/(.*)$  $2?language=$1 [L]

The meaning is rather obvious: take the second match ($2), that is what comes after the second slash in the URL, postpone a ?language= after it, and then the first match ($1), that is, what’s between the first two slashes – provided it’s one of en or de.
If you wanna match anything (not only en or de) as language, then change the rule to:

RewriteRule ^/(.+?)/(.*)$  $2?language=$1 [L]

Please note: the ? in the first group will make the match stop at first slash, so that you won’t rewrite eg.

/en/subdir/pippo.php

to

pippo.php?language=en/subdir

but rather to

subdir/pippo.php?language=en

In case of doubts, there is an excellent documentation in the Apache website.


Edit: default language

To make all other requests (that is, URLs not starting with /en/ or /de/) redirect to a default language (say en), first you have to know the language prefixes you want to recognize, and then use the following rules – below, I’m assuming there are three -3- languages, with codes en,de and fr:

RewriteEngine On
RewriteBase /

RewriteRule ^(en|de|fr)/(.*)$  $2?language=$1 [L,QSA]
RewriteRule ^(.*)$  $1?language=en [L,QSA]

If you don’t define the exact language set in the first RewriteRule (eg using my previous “any language” solution), language detection could fail on nested pages. It’s important that the rules are in this order because the first one will match pages with the language code and then exit, whilst the second one will be applied only if the first does not match.

Related:  .htaccess in Notepad ++

References