2009-11-03 87 views
0

我用下面的國防部重寫代碼的網址,如:www.site.com/play/543國防部重寫HOWTO

RewriteEngine On 
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L] 

我怎麼能在這擴大,所以我可以有一對夫婦的URL像www.site.com/contactwww.site.com/about

回答

2
RewriteRule ^(play|contact|about)/([^/]*)$ /index.php?$1=$2 [L] 

魔法門做的伎倆。現在請求/ play/foo指向/index.php?play=foo和/ contact/bar指向/index.php?contact=bar等。

編輯,從評論「關於和聯繫永遠不會被設置。」。

然後只用兩個重寫;

RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L] 
RewriteRule ^(contact|about)/?$ /index.php?a_variable_for_your_page=$1 [L] 
+0

關於和聯繫將永遠不會被設置。 – ian 2009-11-03 12:13:41

1

被大多數PHP框架的使用的常見的方式是剛好路過無論用戶的請求,除了現有的文件(一般資產* .png和* .js文件*。的CSS)和/或公共目錄PHP腳本然後解釋PHP端的路由。

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d #if not an existing directory 
    RewriteCond %{REQUEST_FILENAME} !-f #if not an existing file 
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] #pass query string to index.php as $_GET['url'] 
</IfModule> 

在PHP端,以避免這樣的

$page = getPageFromUrl($_GET['url']); 
include($page); 

東西取用戶輸入時所以要非常小心,這是非常重要和消毒/過濾器,以避免遠程用戶訪問非公開您的虛擬主機上的文件。