The site has three localizations. ru , de and en . English is basic. The user first visits the main page of the site at http://mysite.com/ , sending a request Accept-Language: fr;q=0.9 . Since French is not implemented, it redirects it to the English version http://mysite.com/en/ . The site has a search form that sends a POST request to the URL /search . There is no state saving on the server (sessions and analogues)

I tried to implement via .htaccess , but I don’t know how to correctly implement URL generation for forms and how to transfer a locale. And also how to substitute a locale in the URL, if the user clicks on the link without the http://mysite.com/faq locale

 RewriteEngine On #Локаль по умолчанию RewriteCond %{HTTP:Accept-Language} !^(ru|de) [NC] RewriteRule ^$ /en/ [L,R=302] RewriteCond %{HTTP:Accept-Language} ^ru [NC] RewriteRule ^$ /ru/ [L,R=302] RewriteCond %{HTTP:Accept-Language} ^de [NC] RewriteRule ^$ /de/ [L,R=302] #Перенаправлять все запросы на единую точку входа RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php 

    1 answer 1

    Decided using PHP

     class T { public static $lang; public static function init(array $languages, $defaultLanguage, $delimiter = '/') { $url = ltrim($_SERVER['REQUEST_URI'], $delimiter); $parts = explode($delimiter, $url); $lang = array_shift($parts); if (!in_array($lang, $languages)) { $prefLocales = array_reduce(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']), function ($res, $el) { list($l, $q) = array_merge(explode(';q=', $el), [1]); $res[$l] = (float)$q; return $res; }, []); arsort($prefLocales); $lang = $defaultLanguage; foreach (array_keys($prefLocales) as $l) { if (in_array($l, $languages)) { $lang = $l; break; } } header("Location: {$delimiter}{$lang}{$delimiter}{$url}"); exit(); } $_SERVER['REQUEST_URI'] = $delimiter . implode($delimiter, $parts); self::$lang = $lang; } } 

    All requests are sent to a single entry point.

     RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php 

    All links are corrected with the help of JS, or are added to the backend

     <?php T::init(['en','de','ru'], 'en'); ?> <!DOCTYPE html> <html lang="<?= T::getLang() ?>"> <body> <form action="/<?= T::getLang() ?>/search" method="POST" > <input type="text" name="term"/> <button type="submit">Search</button> </form> </body> </html>