如何更改已使用mod_rewrite重写的URL

时间:2016-05-23 10:55:17

标签: php apache .htaccess mod-rewrite

我试图为我的路由修改RewriteRule。我正在使用此页面http://htaccess.mwl.be/进行测试。

请求网址为:

http://myLocalhost.com/backFromPayment/1/?status=CLEARED&amount=1718.21&currency=USD&description=U900T2351&hash=e805f58f6f96d9ebd4e0ea9da69a37c10704d482&id_sale=8938744

.htaccess规则是:

RewriteRule !^index.php - [C]
RewriteRule (.*) index.php?$0

输出1的URL是:

http://myLocalhost.com/index.php?backFromPayment/1/index.php?

此规则适用于所有页面,但适用于块"?status = CLEARED& ......"这个规则是不够的。

所以我评论了以下规则:

#RewriteRule !^index.php - [C]
#RewriteRule (.*) index.php?$0

并添加了这个:

 RewriteCond %{QUERY_STRING} status=(\w+)&amount=(\w+\.{1}\w*)&currency=(\w+)&description=(\w+)&hash=(\w+)&id_sale=(\d+)
 RewriteRule ^(.*)$ /index.php?$0status/%1/amount/%2/currency/%3/description/%4/hash/%5/id_sale/%6

输出2网址为:

http://myLocalhost.com/index.php?backFromPayment/1/status/CLEARED/amount/1718.21/currency/USD/description/U900T2351/hash/e805f58f6f96d9ebd4e0ea9da69a37c10704d482/id_sale/8938744

另一项修改如下:

RewriteCond %{QUERY_STRING} status=(\w+)&amount=(\w+\.{1}\w*)&currency=(\w+)&description=(\w+)&hash=(\w+)&id_sale=(\d+)
RewriteRule ^backFromPayment/1/ http://%{HTTP_HOST}/backFromPayment/1/status/%1/amount/%2/currency/%3/description/%4/hash/%5/id_sale/%6?

输出3网址为:

http://myLocalhost.com/backFromPayment/1/status/CLEARED/amount/1718.21/currency/USD/description/U900T2351/hash/e805f58f6f96d9ebd4e0ea9da69a37c10704d482/id_sale/8938744

我的修改不起作用。但我注意到,当我手动修改URL(复制/粘贴输出3)时,URL工作。

所以我必须修改.htaccess的这一部分:

RewriteRule !^index.php - [C]
RewriteRule (.*) index.php?$0

如何修改此部分?

1 个答案:

答案 0 :(得分:0)

RewriteRule的替换部分中添加查询字符串时,此新查询字符串将覆盖任何以前的查询字符串。如果要保留以前的查询字符串,请添加QSA标记,例如

RewriteRule .* index.php?$0 [QSA]