2011-11-11 28 views
0

這是一種基於URL包含頁面的可怕方式嗎? (使用mod_rewrite通過index.php在PHP中包含基於URL的頁面

if($url === '/index.php/'.$user['username']) { 
    include('app/user/page.inc.php'); 
} 

// Upload * 
else if($url === '/index.php/'.$user['username'].'/Upload') { 
    include('app/user/upload.inc.php'); 
} 

// Another page * 
else if($url === '/index.php/AnotherPage') { 
    include('page/another_page.inc.php'); 
} 

我使用$_GET['variables']通過mod_rewrite

^(.+)$ index.php?user=$1 [NC] 

和其他幾個基地的網頁。但是,這些僅僅是基礎文件的第一個參數。上面的if/else例子也是區分大小寫的,這真的不好。

你對此有何看法?

我該如何mod_rewrite這些2nd/3rd等參數關閉index.php

這是完全不符合上述例子的SEO嗎?

+0

這包括運行此代碼的函數範圍內的文件。那是你要的嗎?請參見[PHP手冊](http://php.net/manual/en/function.include.php)中的示例2。 –

回答

0

我不完全理解你的問題,本身。
你是什麼意思「這些第二/第三等參數」?

可以做在一個更可讀的/可維護的方式相同的步驟如下:

$urls = array(
'/index.php/'.$user['username']   => 'app/user/page.inc.php', 
'/index.php/'.$user['username'].'/Upload' => 'app/user/upload.inc.php', 
'/index.php/AnotherPage'     => 'page/another_page.inc.php' 
); 
$url = $urls[$url]; 

如果「.inc.php」是洽,可以從陣列的每個項目刪除和添加它底:
$url = $urls[$url].'inc.php'

沿着相同的線路,可以寫在反向陣列(開關在上述陣列的鍵和值),並使用preg_grep搜索它。這將允許您搜索網址而不區分大小寫,並允許使用通配符。

$url = key(preg_grep("/$url/i", $urls)); 

查看Here瞭解實時交互式示例。

請注意,這是效率低得多,但通配符匹配是最好的方法。
(對於大多數頁面來說,效率低下是宜居的。)

相關問題