重写一个链接mod_rewrite

时间:2012-09-21 11:46:57

标签: php html apache mod-rewrite

在商店里,我有类别和产品,例如。类别形成为

localhost/categories.php?categoryid=***

其中categoryid可以是任何字段。

产品形成为

localhost/products.php?categoryid=***&productmanufactor=***&productname=***

其中categoryidproductmanufactorproductname也可以是任何字词

所以我想从(示例)重写:

localhost/carlocalhost/categories.php?categoryid=car除了某些词语(关于,联系人等)

localhost/car/audi/r8 

localhost/products.php?categoryid=car&productmanufactor=audi&productname=r8

如何轻松完成这项工作?

htaccess的

RewriteEngine On
RewriteBase /
RewriteRule ^about$           about.php [NC,L]
RewriteRule ^contact$         contact.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$            categories.php?categoryid=$1 [NC,L]

2 个答案:

答案 0 :(得分:4)

RewriteRule ^(.*)/(.*)/(.*)$  products.php?categoryid=$1&productmanufactor=$2&productname=$3 [NC,L]
RewriteRule ^about$           about.php [NC,L]
RewriteRule ^contact$         contact.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$            categories.php?categoryid=$1 [NC,L]

答案 1 :(得分:2)

下面的代码可能有所帮助。

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /products.php?categoryid=$1&productmanufactor=$2&productname=$3 [L]
RewriteRule ^([^/]*)$ /categories.php?categoryid=$1 [L]

同时检查http://www.generateit.net/mod-rewrite/我正在使用此网站生成htaccess,使用起来很简单。