2012-05-14 88 views
0

我試圖找出一個URL重寫,但我完全沒有用重寫規則。使用.htaccess重寫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 

如何將重寫規則是什麼樣子?

感謝您的任何幫助和/或建議!

回答

1

如果您不想將所有可能的翻譯列表(catalog-katalog,view-something等)包含到.htaccess文件中,則創建單獨的php腳本將更容易,該腳本將處理路由。例如:

RewriteRule ^(en|de)/([^/]+)/(.+)\.html$ router.php?lang=$1&section=$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"; 
} 
+0

你好,非常感謝您的回覆!很好的解決方案,但最好我希望它在.htaccess –

+0

如果你只有幾個部分,你可以做一些像'RewriteRule^en/SECTIONNAME /(。+)\。html $ SECTIONSCRIPT.php?lang = en&id = $ 1 [ L,QSA]''爲每種語言和部分組合,例如:'RewriteRule^de/katalog /(。+)\。html $ catalog.php?lang = de&id = $ 1 [L,QSA]' –