VirtualHosts上的Apache RewriteRule不适用于组捕获正则expression式

我需要创build一个RewriteRule来将像/tdg/image.jpg?mode=crop&width=300&height=300这样的URLpath/tdg/image.jpg?mode=crop&width=300&height=300给本地代理。

代理需要将给定的URL转换为以下格式。

 http://localhost:8888/unsafe/300x300/smart/tdg/image.jpg 

我先尝试使用ProxyPassMatch apache指令,但是我无法从查询string中检索宽度和高度数据。

 ProxyRequests On ProxyPreserveHost On ProxyPassMatch ^\/(tdg.+\.(?:png|jpeg|jpg|gif))\?mode=crop.+width=(d+)&height=(d+) http://localhost:8888/unsafe/$2x$3/smart/$1 

我也试过RewriteRule

 RewriteEngine On RewriteRule ^\/(tdg.+\.(?:png|jpeg|jpg|gif))\?mode=crop.+width=(d+)&height=(d+) http://localhost:8888/unsafe/$2x$3/smart/$1 

在这两种情况下,代理的结果URL都是http://localhost:8888/unsafe/x/smart/$1 ,其中应该是http://localhost:8888/unsafe/300x300/smart/tdg/image.jpg

我不知道为什么我不能使用group正则expression式从查询string中获取widthheight值。

RewriteRule指令只匹配path组件,不包含查询string。 尝试:

 RewriteEngine On RewriteCond %{QUERY_STRING} mode=crop.+width=(\d+)&height=(\d+) RewriteRule ^\/(tdg.+\.(?:png|jpeg|jpg|gif)) http://localhost:8888/unsafe/%1x%2/smart/$1 [P] 

使用RewriteCond时,请注意replace中的反向引用的差异。 为了使用来自这两个地方的反向引用,对RewriteCond那些使用%N,对RewriteRule中的使用$ N。