2017-07-25 155 views
0

我想爲我正在處理的網站設置自定義模板系統,因此我想將所有請求重定向到單個文件。如果用戶訪問像http://www.mywebsite.de/products/software/我想他的請求重定向至index.php文件的URL,所以我可以做類似如下:.htaccess從路徑重定向到文件

$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 
} 

然後,我可以用include來,還有包括其他的HTML文件根據所要求的內容,輸入index.php

最後,像http://www.mywebsite.de/products/software/的請求被認爲是相同http://www.mywebsite.de/index.php?first_level=products&second_level=software

回答

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 

接着在PHP例如:

$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));