views:

317

answers:

4

What's the easiest way to convert these requests:

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

To:

http://www.example.com/index.php?language_id=en

There's various PHP files, so the rule would also need to map

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

To:

http://www.example.com/anyfile.php?language_id=en

The language ID can be any two letter code (it's checked within the PHP script, to ensure it's valid).

Presumably, I'll need to use .htaccess, and any advice on how to implement the above would be greatly appreciated. Thanks.

Update

The following worked for me.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ([a-z]+)/(css|js|images)/(.+) /$2/$3 [L]
  RewriteRule ([a-z]+)/(.+).php /$2.php?language_id=$1 [QSA,L]
  RewriteRule ([a-z]+)/$ /index.php?language_id=$1 [QSA,L]
</IfModule>

This also re-routes requests to the css, js, and images folders back to the root folder. It also works if no filename is entered, automatically redirecting to index.php.

A: 
RewriteRule /en/(.*) /$1?language_id=en

It's untested but in theory it should map anything after en to be a variable becore the ?language_id=en

You could also do

RewriteRule /([a-zA-Z0-9+])/(.*) /$2?language_id=$1

This should also map

http://www.example.com/is/index.php

To

http://www.example.com/index.php?language_id=is

Ólafur Waage
+1  A: 
RewriteRule /([^/]+)/(.*) /$2?language_id=$1
vartec
+1  A: 

You probably want to add the QSA flag to your rewrite rules, which will append any addiitonal bits on the query string to the rewritten request, e.g.

RewriteRule magic here [PT,QSA]
Rob
Thanks, the QSA flag is what I was looking for :-)
Mun
thanks, I always wondered what that flag was for
Litso
A: 

I'm using following construct for friendly URL's:

RewriteRule ^([^/]+)[/]{0,1}(.*)$ index.php?q=$2&language=$1 [L,QSA,NC]
feeela