如何为带有参数的url执行mod_rewrite


How to do mod_rewrite for a url with parameters

假设我的网站中有以下url:http://localhost/test/example.php?a=1&b=2

如何在.htaccess文件中指定重定向规则,以便在浏览器中输入以下内容时调用此url:http://localhost/test/example/1.html

换句话说,如果用户输入url:http://localhost/test/example/1.html,他应该显示页面:http://localhost/test/example.php?a=1&b=2

我在.htaccess文件中尝试了以下mod_rewrite代码,但它不起作用:

RewriteRule ^/([^/]+).html /example.php?a=$1&b=$2 [QSA]

这是我.htaccess文件中的代码:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/? /example.php?a=$1&b=$2 [QSA]

注意:我使用的是WAMP2.0、Apache 2.2.6和PHP 5.2.5

它不起作用,因为您的规则中有错误,您必须在开始时删除斜杠:

RewriteRule ^test/example/([0-9]+)'.html? /test/example.php?a=$1&b=2 [QSA]