Please help me with php file that must redirect to correct language.
the site architecture is
site.com/en
site.com/fr
my cod:
<?php
$sites = array(
"en" => "/en/index",
);
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (!in_array($lang, array_keys($sites))){
$lang = 'en';
}
// перенаправление на субдомен
header('Location: ' . $sites[$lang]);
?>
You could to append the
$_SERVER['PHP_SELF']variable, that contains the path relative to the document root, to your language folder.If your path were
site.com/fr/file, this variable would contain/fr/file. When you remove the first part of this part, you get the ‘language-independent’ path of the called script, that you can append to the desired language directory.If your path is as you described, you could try the following solution (untested!):
EDIT: to clarify the approach:
If the site is
site.com/fr/123/index.php, the variable$path_to_scriptwill then contain/123/index.php, the script directory without the leading language dir.You can then append this to the desired language dir and get the path you wanted:
/en/123/index.php