PHP
Router PHP semplice per URL pulite
Come leggere REQUEST_URI e instradare /guide/cat/slug/ verso un file di rendering.
Idea generale
Con .htaccess rimandi tutto a index.php. Poi in PHP leggi REQUEST_URI e fai match con regex.
$path = explode('?', $_SERVER['REQUEST_URI'], 2)[0];
$path = rtrim($path, '/');
if($path==='') $path = '/';
if(preg_match('#^/guide/([a-z0-9\-]+)/([a-z0-9\-]+)$#', $path, $m)){
$cat = $m[1];
$slug = $m[2];
// carica JSON e renderizza
}
Sicurezza base
Normalizza sempre cat/slug per evitare path traversal. Mai usare direttamente input per costruire path senza pulizia.
function normalize_slug($s){
$s = strtolower(trim($s));
$s = preg_replace('/[^a-z0-9\-]+/', '-', $s);
$s = preg_replace('/\-+/', '-', $s);
return trim($s, '-');
}