多的RewriteCond在重写规则的参考捕捉组 [英] Reference capture groups of multiple RewriteCond in RewriteRule

查看:561
本文介绍了多的RewriteCond在重写规则的参考捕捉组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有多个的RewriteCond链接在一起,最后的RewriteCond只捕获组可以使用%,0 引用C $ C>。

When I have multiple RewriteCond chained together, only the capture groups of the last RewriteCond can be referenced with %0-%9.

在下面的问题中的URL的查询串中的参数可以是任何顺序。为了解析成一个奇特的URL我需要每个参数在单独的查询字符串匹配:

In the following problem the parameters in the query string of the url can be in any order. To parse them into a fancy url I would need to match each parameter in the query string individually:

RewriteCond %{QUERY_STRING} param1=([^&]+)
RewriteCond %{QUERY_STRING} param2=([^&]+)
RewriteCond %{QUERY_STRING} param3=([^&]+)
RewriteRule ^foo$ bar/%1/%2/%3/ [R]

正如我指出...这是行不通的。为了解决这个问题,我可以参考previous的RewriteCond的捕获组在未来的RewriteCond和'传播'每个参数的实际重写规则:

Like I pointed out... this doesn't work. To fix this, I could reference the capture group of the previous RewriteCond in the next RewriteCond and 'propagate' each parameter to the actual RewriteRule:

RewriteCond %{QUERY_STRING} param1=([^&]+)
RewriteCond %1&%{QUERY_STRING} ^([^&]*)&.*param2=([^&]+)
RewriteCond %1&%2&%{QUERY_STRING} ^([^&]*)&([^&]*)&.*param3=([^&]+)
RewriteRule ^foo$ bar/%1/%2/%3/ [R]

这应该工作,但每增加一个参数,它得到的混乱。另一个解决方法也可能会被解析一个参数和调整客户端的每个参数(导致冗长的链条重定向,这是我想避免的)之后。

This should work, but for each additional parameter it get's messier. An other solution could possibly be parsing one parameter and redirecting the client after each parameter (resulting in a lengthy chain of redirects, which I would like to avoid).

有没有访问在重写规则中所有的RewriteCond的捕获组的更清洁的方式(如是否有可能命名或将其分配给一个变​​量,所以我可以参考别的地方?)

Is there an cleaner way of accessing the capture groups of all RewriteCond's in the RewriteRule (e.g. is it possible to name them or assign them to a variable so I can reference them somewhere else?)

推荐答案

您可以尝试构建的目标URL内部的重写条件:

You could try constructing the target URL inside the rewrite conditions:

RewriteCond ##%{QUERY_STRING}      (.*)##(|.*&)param1=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param2=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param3=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param4=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param5=([^&]+)
RewriteCond %1/%3##%{QUERY_STRING} (.*)##(|.*&)param6=([^&]+)

RewriteRule ^foo$ /bar%1/%3? [L,R]

当我尝试要求:

/富参数1 = A和?参数2 = B和param6 = 3及参数3 = 4和param5 = 5和param4 = 6

我重定向到:

/酒吧/ A / B / 4/6/5/3

添加任何额外的需要的查询字符串参数不会让它看起来更多的凌乱比它已经是了。

Adding any additional required query string parameters won't make it look any more messy than it already is.

这篇关于多的RewriteCond在重写规则的参考捕捉组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆