N0lf N0lf - 2 months ago 10
Apache Configuration Question

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.

I want to rewrite an URL with a GET language parameter to a more clean URL.

For instance :
http://www.example.com?lang=en

Needs to be =
http://www.example.com/en

this works fine with this rewrite rule :

RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]


But I can't get it to work on URLs like these :

http://www.example.com/contact.php?lang=en

http://www.example.com/about.php?lang=en

That need to be :

http://www.example.com/en/contact.php

http://www.example.com/en/about.php

Anyone has an idea what I'm missing in my rewrite rule to make this work ?

Thanks in advance !

Answer

You will need an additional rewrite rule for handling /en/about.php:

RewriteEngine On

RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]

RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]