重写查询字符串
我正在尝试编写一些 mod_rewrite 规则来动态生成缩略图。 因此,当这个 url 时,
example.com/media/myphoto.jpg?width=100&height=100
脚本应该将其重写
example.com/media/myphoto-100x100.jpg
,如果文件存在于磁盘上,则由 Apache 提供服务,如果不存在,则调用脚本来生成文件。
我写了这个
RewriteCond %{QUERY_STRING} ^width=(\d+)&height=(\d+)
RewriteRule ^media/([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9]+)$ media/$1-%1x%2.$2 [L]
RewriteCond %{QUERY_STRING} ^(.+)?
RewriteRule ^media/([a-zA-Z0-9_\-\._]+)$ media/index.php?file=$1&%1 [L]
,我得到了无限的内部重定向。 第一个条件匹配并且规则被执行,之后我就得到了内部重定向。
我需要建议来完成这个脚本。
谢谢。
I am trying to write some mod_rewrite rules to generate thumbnails on the fly.
So when this url
example.com/media/myphoto.jpg?width=100&height=100
the script should rewrite it to
example.com/media/myphoto-100x100.jpg
and if the file exists on the disk it gets served by Apache and if it doesn't exist it is called a script to generate the file.
I wrote this
RewriteCond %{QUERY_STRING} ^width=(\d+)&height=(\d+)
RewriteRule ^media/([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9]+)$ media/$1-%1x%2.$2 [L]
RewriteCond %{QUERY_STRING} ^(.+)?
RewriteRule ^media/([a-zA-Z0-9_\-\._]+)$ media/index.php?file=$1&%1 [L]
and I get infinite internal redirects.
The first condition is matched and the rule is executed and right after that I get an internal redirect.
I need advice to finish this script.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将其用于第二个条件:
RewriteCond
Try using this for your second condition:
RewriteCond
尝试这些规则:
第一个规则的第二个条件测试是否存在具有该名称的文件 (
-f
)。Try these rules:
The second condition of the first rule tests if there is a file with that name (
-f
).