当 2 个条件 URL 重写规则同时触发时出现致命错误
亲爱的朋友们, 尝试使用重写规则重写图像,该规则仅在存在两个条件时触发:图像以 IMG
或更好的 IMG-
开头,其次文件应该存在。
漂亮的网址:IMG-folder/file.jpg
在水下获取了丑陋的网址图像。
以下代码在未设置条件以及仅设置第二个 !-f 条件时有效,但在两个规则共存时无效。这是为什么呢?非常感谢想法/代码/评论。谢谢
Q1。我有点困惑:第一个条件真的有必要吗?因为我声明只将 IMG-.... 文件重写到水下的丑陋网址。
Q2。为什么下面的代码会出错,然后两个条件都会被触发?
RewriteCond %{REQUEST_URI} ^IMG.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^IMG-(.+)_w(.+)_h(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3 [L]
Dear folks,
Trying to rewrite images with a rewrite rule that should only fire when two conditions exist: image starts with IMG
or better yet IMG-
and secondly the file should exist.
Nice url: IMG-folder/file.jpg
fetched ugly url image under water.
The code below works when no conditions are set, as well as when only the second !-f condition is set, but not when both rules coexist. Why is this? Ideas/code/comments are very appreciated. Thanks
Q1. Im a bit confused: is the first condition actually necessary? Since I'm stating to only rewrite IMG-.... files to the ugly urls under water.
Q2. Why does this code below go wrong then both conditions are fired?
RewriteCond %{REQUEST_URI} ^IMG.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^IMG-(.+)_w(.+)_h(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
%{REQUEST_URI}
,始终包含完整的请求路径。另一方面,RewriteRule
的第一个参数是相对的。如果将%{REQUEST_URI}
与指定起始位置 ^ 的模式进行匹配,则还应该包含前导斜杠。我对虚拟 URL 的形式也有点困惑。 IMG 在典型的图像 URL 中出现两次吗?
/IMG-folder/IMG-12_w34_h56.jpg
是有效的虚拟文件名吗?%{REQUEST_URI}
, always contains the full requested path. The first argument ofRewriteRule
on the other hand is relative. If you match a%{REQUEST_URI}
against a pattern that specifies the start position, ^, you should also include the leading slash.I'm also slightly confused about the form of the virtual URLs. Does IMG occur twice in a typical image URL? Is
/IMG-folder/IMG-12_w34_h56.jpg
a valid virtual filename?