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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…