views:

394

answers:

3

I'm writing multilingual website. I have several files on server like:

/index.php
/files.php
/funny.php

And would like to add language support by placing language code into URL like this:

http://mywebsite/en/index.php

would redirect to:

http://mywebsite/index.php?lang=en

And

http://mywebsite/en/files.php

would redirect to:

http://mywebsite/files.php?lang=en

I would like to put more languages for example:

http://mywebsite/ch-ZH/index.php

And I would like this to work only for files with php and php5 extension. Rest of files should be the same as they are.

So for example when i will go to address

http://mywebsite/ch-ZH/index.php

I would like my PHP to recognize that current path is

http://mywebsite

and NOT

http://mywebsite/ch-ZH

It's necessary for me because in my PHP code I relate on current path and would like them to work as they are working now.

Could you please write how to prepare htaccess file on Apache to meet this criteria?

+1  A: 

Something like this should do the trick,

RewriteEngine on
RewriteRule ^(.[^/]+)/(.*).php([5])?$ $2.php$3?lang=$1 [L]

This will only match .php and .php5 files, so the rest of your files will be unaffected.

Rich Adams
+4  A: 

Try this:

RewriteEngine on
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]

And for the current path problem, you have to know how relative URIs are resolved: Relative URIs are resolved by the client from a base URI that is the URI (not filesystem path!) of the current resource if not declared otherwise.

So if a document has the URI /en/foo/bar and the relative path ./baz in it, the client resolves this to /en/foo/baz (as obviously the client doesn’t know about the actual filesystem path).
For having ./baz resolved to /baz, you have to change the base URI which can be done with the HTML element BASE.

Gumbo
+1  A: 

If you don't want to use ModRewrite, you can put symbolic links in the web folder (ln -s . en) and check the URL in PHP.

too much php