使用.htaccess重写URL(多语言)
我试图找出一个URL重写,但我完全没有重写规则 我希望:使用.htaccess重写URL(多语言),.htaccess,url,rewrite,.htaccess,Url,Rewrite,我试图找出一个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 重写规则是什么样子的 感谢您的帮助和/或建议 如果您不想在.htaccess文件中包
/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
重写规则是什么样子的
感谢您的帮助和/或建议 如果您不想在.htaccess文件中包含所有可能的翻译(目录katalog、查看某些内容等)的列表,则更容易创建单独的php脚本,该脚本将处理路由。例如:
RewriteRule ^(en|de)/([^/]+)/(.+)\.html$ router.php?lang=$1§ion=$2&id=$3 [L,QSA]
<?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";
}
例如,router.php可以是:
RewriteRule ^(en|de)/([^/]+)/(.+)\.html$ router.php?lang=$1§ion=$2&id=$3 [L,QSA]
<?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";
}
您好,非常感谢您的回复!很好的解决方案,但最好是在.htaccess中使用它。如果您只有几个部分,您可以对每种语言和部分组合执行类似RewriteRule^de/katalog/(.+)\.html$SECTIONSCRIPT.php?lang=en&id=$1[L,QSA]
的操作,例如:RewriteRule^de/katalog/(.+)\.html$catalog.php?lang=de&id=$1[L,QSA]