重写规则为图像文件,则仅请求的查询字符串是present重写、字符串、图像文件、规则

2023-09-02 00:54:50 作者:无戏配角

使用我有限的普通恩pression和mod_rewrite的能力,我试图重写某些图像请求,所以我可以改变一个PHP脚本的输出。这是我有:

With my limited regular expression and mod_rewrite abilities, I'm attempting to rewrite certain image requests so I can alter the output with a php script. Here's what I have:

RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(JPEG|JPG|GIF|PNG|jpeg|jpg|gif|png)$ public/images.php?%{QUERY_STRING}&src=$1/$2.$3 [L]
#            [       1      ] [       2       ] [                  3              ]

这不工作,但它太贪婪,没有的需要的查询字符串,这是很重要的 - 否则,所有的图像请求将被改写。我试图把一个?(。*)的规则,我要么得到一个内部服务器错误或者它似乎没有解决问题(最有可能的,因为我没有正确地做到这一点)。我也试过%{QUERY_STRING} 在条件的结尾,但似乎并没有影响到任何东西。

This does work, but it's too greedy and doesn't require the query string, which is important - otherwise all images requests would be rewritten. I tried putting a ? or ?(.*) in the rule, and I would either get an internal server error or it didn't seem to solve the problem (most likely because I didn't do it correctly). I also tried %{QUERY_STRING} at the end of the condition, but that didn't seem to affect anything.

下面是我希望发生的:

所有的请求公共/ 上传/ ... 在随后的任何路径的图像(文件扩展名不区分大小写)... 在随后的查询字符串... ...应该重写公共/ images.php 与原来的查询字符串,并添加一个aditional的参数:的src ,它包含了实际的图像的路径(在改写部分)。 额外将待好,但不是必要的:限制该规则仅重写网址如果查询字符串包含从一组参数中的至少一个项目。例如,只有当一个宽度= 高度= 相比之下= PARAMS是present。如果这使事情变得臃肿和复杂,我不担心。 Any requests for public/ or uploads/... Followed by any path to an image (file extension case insensitive)... Followed by a query string... ...should rewrite to public/images.php with the original query string, and add one aditional parameter: src, which contains the actual path to the image (the rewritten part). Extra "would-be-nice", but not necessary: Restrict the rule to only rewrite the url if the query string contains at least one item from a set of parameters. For example, only if one of the width=, height= or contrast= params are present. If this makes things bloated or complicated, I'm not worried about it.

因此​​,例如,对于一个请求:

So for example, a request for:

uploads/images/my_folder/test.jpg?width=320&height=220

应该由提供服务:

Should be served by:

public/images.php?width=320&height=220&src=public/images/my_folder/test.jpg

的.htaccess 文件在我的根目录下,还有公开上传目录。

The .htaccess file is in my root directory, as well as the public and uploads directories.

我想避免绝对URL,因为我想这是便携式,而无需修改。我已经做了很好的处理google搜索和阅读SO相关的帖子,并且还是想不通这一个。我怎样才能修补这个规则,做我想做的,还是有更好的办法做到完全写?

I want to avoid absolute urls, because I want this to be portable without needing to edit. I've done a good deal of googling and reading related SO posts, and still can't figure this one out. How can I patch this rule to do what I want, or is there a better way to do write this altogether?

修改:只是要注意,这个规则previously为我工作:

Edit: Just want to note that this rule worked for me previously:

RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(JPEG|JPG|GIF|PNG|jpeg|jpg|gif|png)/([0-9]+)$ public/images.php?width=$4&src=$1/$2.$3

......但只有像上传/ my_folder / image.jpg文件/ 280 请求 - 我用280的宽度,但现在我想接受组合没有特定的顺序多个参数:

...but only for requests like uploads/my_folder/image.jpg/280 - I used the 280 as the width, but now I want to accept combinations of multiple parameters in no particular order:

推荐答案

有两种方法:

1 添加条件,只有当重写查询字符串不为空(可以是任何东西):

1. Add a condition to only rewrite when query string is not empty (can be anything):

RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(jpe?g|gif|png)$ public/images.php?src=$1/$2.$3 [NC,QSA,L]

2 添加条件,只有当重写查询字符串是不是空的,拥有这些参数present至少一个(但该值可以为空):

2. Add a conditions to only rewrite when query string is not empty and has at least one of those parameters present (but the value can be empty):

RewriteCond %{QUERY_STRING} (^|&)width=([^&]*)(&|$) [OR]
RewriteCond %{QUERY_STRING} (^|&)height=([^&]*)(&|$) [OR]
RewriteCond %{QUERY_STRING} (^|&)contrast=([^&]*)(&|$)
RewriteRule ^(public|uploads)/([A-Za-z0-9/_-]+).(jpe?g|gif|png)$ public/images.php?src=$1/$2.$3 [NC,QSA,L]

我还没有真正测试#2 ..但应该可以正常工作(从全工作code复制)。

I have not really tested #2 .. but should work fine (copied from fully working code).

注:

您可以替换 ^(公众|上传)/([A-ZA-Z0-9 / _-] +)(JPE G |?GIF | PNG)。$ ^((?:公共|上传)/(?:?[A-ZA-Z0-9 / _-] +)(?: JPE G | GIF | PNG) )$ ..然后,而不是和 SRC = $ 1 / $ 2,$ 3 只使用 SRC = $ 1

You can replace ^(public|uploads)/([A-Za-z0-9/_-]+).(jpe?g|gif|png)$ by ^((?:public|uploads)/(?:[A-Za-z0-9/_-]+).(?:jpe?g|gif|png))$ .. and then instead of src=$1/$2.$3 use just src=$1

或者 - 替换 ^(公众|上传)/([A-ZA-Z0-9 / _-] +)(?JPE G | GIF | PNG)$ ^(?:公共|上传)/ [A-ZA-Z0-9 / _-] + (?: JPE G |?GIF | PNG)。$ 然后用 SRC = $ {REQUEST_URI} - 唯一不同的是 SRC = 参数将启动与领先的斜杠。

Alternatively -- replace ^(public|uploads)/([A-Za-z0-9/_-]+).(jpe?g|gif|png)$ by ^(?:public|uploads)/[A-Za-z0-9/_-]+.(?:jpe?g|gif|png)$ and then use src=${REQUEST_URI} -- the only difference that src= parameter will start with leading slash.