2

I need to create a RewriteRule to delegate an URL path like /tdg/image.jpg?mode=crop&width=300&height=300 to a local proxy.

The proxy needs the given URL be transformed into the following format.

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

I tried first using ProxyPassMatch apache directive but I can't retrieve the width and height data from the query 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

I also tried RewriteRule

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

And in both cases, the result URL for the proxy is http://localhost:8888/unsafe/x/smart/$1 where should be http://localhost:8888/unsafe/300x300/smart/tdg/image.jpg

I have no clue why I can't fetch the width and height value from the query string, using group regex syntax.

1 Answers1

3

The RewriteRule directive only matches on the path component, it does not include the query string. Try:

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]

Note the difference of the back-reference in the replacement when using RewriteCond. In order to use back-references from both places, use %N for those from RewriteCond and $N for those in the RewriteRule.

Jeff Snider
  • 3,272
  • 18
  • 17
  • Yeah, I had read about `RewriteCond` but I had undestood that was just for a check condition before the `RewriteRule` be evaluated – Jonas Porto Oct 17 '17 at 17:28
  • 1
    Also, was needed escape the regex group `(\d+)` in `RewriteCond %{QUERY_STRING} width=(\d+)&height=(\d+)` – Jonas Porto Oct 17 '17 at 17:30