简体   繁体   中英

URL Rewrite (multi language) with .htaccess

I'm trying to figure out a URL Rewrite, but I'm totally useless with rewrite rules.

I want the following:

/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

How would the rewrite rule look like?

Thanks for any help and/or suggestions!

If you do not want to include list of all possible translations (catalog-katalog, view-something, etc.) into .htaccess file, it is easier to create separate php script, which will handle routing. For example:

RewriteRule ^(en|de)/([^/]+)/(.+)\.html$ router.php?lang=$1&section=$2&id=$3 [L,QSA]

router.php could be for example:

<?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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM