I still have no answer about this so I’m posting it here.
I want to match an url in the form of root/language/sub/.../document in a .htaccess file and then rewrite it as root/sub/.../document.php?lang=language
Im close to hit but it seems my regexp doesn’t ‘catch’ the input url. Here it is :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ^[a-z]{2}.*$/%{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z]{2})/(.*)$ $2.php?lang=$1
Can someone point me out what is wrong here ? I’m no expert in regexp nor apache rewrites.
Tyvm
* EDIT *
root stands for domain ie mydomain.net
Here are a few examples :
mydomain.net/fr/contact
should be rewriten to
mydomain.net/contact.php?lang=fr
and
mydomain.net/en/articles/view
should be rewriten
mydomain.net/articles/view.php?lang=en
etc…
I believe you are looking for this configuration:
Explanation:
Checks if requested url is not a directory. If yes, no rewriting will be processed.
Checks if url contains
/xx/part inside, wherexxis oneen,deorfr. Flag[NC]allows uppercase and lowercase charactes, so for exampleENorFrwill be accepted. If there is no such “language” part in the url, no rewriting will be processed.Checks if
%1%3.phpfile exists, where%1is(.*/|)and%3is(.*)matches from the previous RewriteCond^(.*/|)(en|de|fr)/(.*)$. If such php file does not exist, no rewriting will be processed.In the RewriteRule left condition will be matched if it came to this line, as it was already checked with RewriteCond, so now it will process rewriting to php file, adding lang part, but because there is also
[QSA]flag, it will keep also other GET parameters, so lang will be added to existing parameters. Flag[L]says it is last rewriting rule that should apply and no more rewriting will be processed with this url.