2010-12-18 90 views
0

我想以这种形式重写我的网址:/ items/category/item-name-id(id是一个数字)。现在我的URL如下所示:items.php?cat = category & name = item-name & id = 12321如何使用mod重写将我的URL重写为items/category/item-name-itemid?

哦,我需要放置mod-rewrite的代码?

+0

juat在您的代码中重写它,将items.php?cat = category&name = item-name&id = 12321更改为/ items/category/item-name-id。当然,你需要用一些重写规则来备份它。 – 2010-12-18 10:36:30

回答

1

在.htacces:

RewriteEngine On 

# Enable these for debugging, disable when done (it's "expensive") 
# RewriteLog /var/log/apache2/rewrite.log 
# RewriteLogLevel 9 

# if the requested URI doesn't point to an existing file or directory 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# .. pass it off to items.php (or index.php, or..) with the path as q, 
# and whatever other GET parameters were there 
RewriteRule ^(.*)$ items.php?q=$1 [QSA] 

的mod_rewrite必须启用明显和Apache的conf需要给你
有 “AllowOverride Options”

有 “AllowOverride全部”

现在字符串“/ items/category/item-name-id”将作为GET参数'q'到达您的脚本,您可以根据需要在'/'和' - '上分割,为每个元素执行表格查找等。

我倾向于通过q数组(它分裂后)到第一个对象(类类),它加载第二个(名称类),加载第三等。但这完全取决于你的'重新去做他们。

+0

+1为推荐的解决方案。 – 2010-12-18 11:24:27