I’d like to know if it is possible to make fake subfolders for my multilanguage website, right now I use this code to switch between the two languages I have (Italian and English)
<?php
session_start();
$langs = [ "en", "it" ];
$current_lang = "en";
if (isset($_GET["lang"]))
{
$lang = $_GET["lang"];
if (in_array($lang, $langs, true))
$current_lang = $lang;
}
elseif (!isset($_SESSION["lang"]))
{
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
{
$acceptLang = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
if (strlen($acceptLang) > 2)
{
$acceptLang = strtolower(substr($acceptLang, 0, 2));
if (in_array($acceptLang, $langs, true))
$current_lang = $acceptLang;
}
}
}
else
$current_lang = $_SESSION["lang"];
$_SESSION["lang"] = $current_lang;
require_once "languages/$current_lang.php";
?>
So whenever I want to switch language I add to the end of the URL ?lang=neededLanguage.php
I also use these rewrite rules to hide the .php
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
This is what I have to do to switch language right now:
https://example.com/?lang=en
https://example.com/blog?lang=en
I’d like to know if it’s possible to make the previous two URLs look something like these:
https://example.com/en/
https://example.com/en/blog