2017-07-25 16 views
0

Ich möchte ein benutzerdefiniertes Template-System für eine Website einrichten, an der ich arbeite, also möchte ich alle Anfragen in eine einzige Datei umleiten. Wenn der Benutzer eine URL wie http://www.mywebsite.de/products/software/ besucht ich wie index.php seine Anfrage in eine Datei umleiten möchten, so konnte ich so etwas wie das folgende tun:.htaccess Redirect von Pfad zu Datei

$look_at = $_GET['first_level']; //for example: products 
if($look_at == 'products') { 
    $look_at = $_GET['second_level']; //here: software 
    if($look_at == 'software') { 
     //show software specific stuff 
    } else if ($look_at == 'hardware') { 
     //show hardware specicif stuff 
    } else { 
     //show error message 
    } 
} else { 
    //show error message 
} 

Dann könnte ich include verwenden, gut sind, andere HTML-Dateien in index.php je nachdem, was angefordert wird.

Am Ende wird eine Anforderung wie http://www.mywebsite.de/products/software/ soll das gleiche sein wie http://www.mywebsite.de/index.php?first_level=products&second_level=software

Antwort

1
RewriteEngine on 

RedirectMatch 403 /\..*$ 
# If the directory or file exists, use them directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Otherwise, send a request to the index.php file 
RewriteRule . index.php 

Next in PHP Beispiel:

$URI = $_SERVER['REQUEST_URI']; 

if (($qpos=(strpos($URI,'/?')))!==false) 
    $URI=substr($URI,0,++$qpos); 
elseif (($qpos=(strpos($URI,'?')))!==false) 
    $URI=substr($URI,0,++$qpos); 

$URI = (preg_replace('/^\/|\/$/', '',$URI));