我正在使用一个非常简单直接的会话切换来使我的网站成为双语。 我想让 URL 对 SEO 更友好,并且像这样 - site.com/en/services,以便仅显示查询 da 或 en 的值。最好在 URL 中的页面名称之前。到目前为止,该网站非常简单,没有子目录,我了解到这可能会有所作为。
到目前为止,我没有使用任何路由器,仅使用 .htaccess 配置
语言切换程序:
<?php session_start();
// Define a list of supported languages to avoid arbitrary file inclusion
$supportedLanguages = ['en', 'da'];
if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = "en";
} else if (isset($_GET['lang']) && in_array($_GET['lang'], $supportedLanguages)) {
$_SESSION['lang'] = $_GET['lang'];
}
// Ensure the requested language file exists before requiring it
$languageFile = "languages/" . $_SESSION['lang'] . ".php";
if (file_exists($languageFile)) {
require_once $languageFile;
} else {
// Handle the case where the language file doesn't exist (e.g., fallback to a default language).
// You can add code here to provide a graceful error message or take other appropriate actions.
die("Language file not found.");
}
?>
HTML 切换界面:
<li role="menuitem">
<?php if($_SESSION['lang']=="da"){
echo '<a class="language-switch" href="?lang=en">EN</a>';
}
else if($_SESSION['lang']=="en"){
echo '<a class="language-switch" href="?lang=da">DA</a>';
} ?>
</li>
.htaccess:
# Page not found / 404 set-up
ErrorDocument 404 /404.php
RewriteEngine on
# does not apply to existing directories, meaning that if the folder exists on the server then don't change anything and don't run the Rule!
# RewriteCond %{REQUEST_FILENAME} !-d
# Check for file in directory with .php extension
RewriteCond %{REQUEST_FILENAME}\.php -f
# Here we actually show the page that has the .php extension
RewriteRule ^(.*)$ $1.php [NC,L]
# RewriteRule (.*) routes.php [QSA,L]
DirectoryIndex /index.php
正如大家之前所建议的,为此目的使用一条路线将有助于防止将来出现很多令人头疼的问题。这是一个非常基本的实现,您可以看一下。
index.php:
<?php session_start();
function route($url)
{
switch ($url) {
case '/':
require_once 'languages/' . $_SESSION['lang'] . '.php';
include 'views/home.php';
break;
default:
include '404.php';
break;
}
}
$supportedLanguages = ['en', 'da'];
if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = "en";
} else if (isset($_GET['lang']) && in_array($_GET['lang'], $supportedLanguages)) {
$_SESSION['lang'] = $_GET['lang'];
}
$route = $_SERVER['REQUEST_URI'];
route($route);
home.php(示例内容):
<!DOCTYPE html>
<html lang="<?php echo $_SESSION['lang']; ?>">
<head>
<meta charset="UTF-8">
<title>Language Switch</title>
</head>
<body>
<h1><?php echo $language['welcome']; ?></h1>
<p><?php echo $language['intro']; ?></p>
<ul role="menu">
<li role="menuitem">
<?php if ($_SESSION['lang'] == "da") {
echo '<a class="language-switch" href="?lang=en">EN</a>';
} else if ($_SESSION['lang'] == "en") {
echo '<a class="language-switch" href="?lang=da">DA</a>';
} ?>
</li>
</ul>
</body>
</html>
en.php:
<?php
$language = [
'welcome' => 'Welcome!',
'intro' => 'This is the English version of the page.'
];
da.php:
<?php
$language = [
'welcome' => 'Velkommen!',
'intro' => 'Dette er den danske version af siden.'
];
.htaccess:
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
DirectoryIndex /index.php