.htaccess重定向与多个查询参数

时间:2015-12-01 16:25:06

标签: php apache .htaccess mod-rewrite url-rewriting

我想在我的网站上使用以下网址。

http://mywebsite.com/product.php
to
http://mywebsite.com/product

http://mywebsite.com/product.php?id=5
to
http://mywebsite.com/product/5


http://mywebsite.com/product.php?action=delete&id=5
to
http://mywebsite.com/product/delete/5



http://mywebsite.com/product.php?action=edit&id=3
to
http://mywebsite.com/product/edit/3

我在.htaccess文件中使用此代码

RewriteEngine on

RewriteRule ^product product.php

RewriteRule ^product/([a-zA-Z0-9_-]+)$ product.php?id=$1
RewriteRule ^product/([a-zA-Z0-9_-]+)/$ product.php?id=$1

但问题是当我使用http://mywebsite.com/product/5并使用GET变量显示时, 它没有传递id参数,我如何从我的网址实现多重查询?

1 个答案:

答案 0 :(得分:0)

试试这个htaccess:

RewriteEngine on

RewriteRule ^product/?$ product.php [L]

RewriteRule ^product/([a-zA-Z0-9_-]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/delete/([a-zA-Z0-9_-]+)/?$ product.php?action=delete&id=$1 [NC,L]
RewriteRule ^product/edit/([a-zA-Z0-9_-]+)/?$ product.php?action=edit&id=$1 [NC,L]