我试图找出一个 URL 重写,但我对重写规则完全没用。
我想要以下内容:
/en/catalog/myname.html -> /catalog.php?id=myname&lang=en
/de/katalog/myname.html -> /catalog.php?id=myname&lang=de
/en/view/123 -> /view.php?id=123&lang=en
重写规则会是什么样子?
感谢您的任何帮助和/或建议!
如果您不想将所有可能的翻译列表(catalog-katalog、view-something 等)包含到 .htaccess 文件中,则更容易创建单独的 php 脚本来处理路由。例如:
RewriteRule ^(en|de)/([^/]+)/(.+)\.html$ router.php?lang=$1§ion=$2&id=$3 [L,QSA]
router.php 可以是例如:
<?php
// List of translations
$german = array(
'katalog' => 'catalog',
'something' => 'view',
...
);
// List of available sections
$allowed = array('catalog', 'view');
$section = $_GET['section'];
if ($_GET['lang'] === 'de') {
$section = isset($german[$section])? $german[$section] : NULL;
}
// IMPORTANT: Include PHP file only if section param is listed in $allowed!
// Otherwise attacker could execute any file on the server
// by opening /router.php?section=badfile
if (in_array($section, $allowed)) {
include dirname(__FILE__) . "/$section.php";
} else {
header('HTTP/1.x 404 Not Found');
print "Page not found";
}