How to translate /en/file.php to a .php file? Lang = en in htaccess Apache

I am writing a multilingual site. I have several files on the server, for example:

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

And I would like to add language support by putting the language code in the URL as follows:

http://mywebsite/en/index.php

will be redirected to:

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

AND

http://mywebsite/en/files.php

will be redirected to:

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

I would like to add more languages, for example:

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

And I would like this to work only for files with the php and php5 extension. The rest of the files should be the same as them.

So, for example, when I get to the address

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

I want my PHP to know that the current path

http://mywebsite

and NOT

http://mywebsite/ch-ZH

This is necessary for me because in my PHP code I am in the current path and would like them to work while they work.

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

+3
3

:

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

, URI: URI URI, URI ( !) , .

, URI /en/foo/bar ./baz , /en/foo/baz (, , ).
./baz /baz, URI, HTML- BASE.

+5

- ,

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

.php .php5, .

+1

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

+1
source

Source: https://habr.com/ru/post/1703025/


All Articles