2009-11-03 144 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); 

东西取用户输入时所以要非常小心,这是非常重要和消毒/过滤器,以避免远程用户访问非公开您的虚拟主机上的文件。