重写查询字符串

发布于 2024-08-26 07:57:00 字数 719 浏览 5 评论 0原文

我正在尝试编写一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-09-02 07:57:00

尝试将其用于第二个条件:

RewriteCond %{REQUEST_FILENAME} !-f

'-f'(是常规文件)
将 TestString 视为路径名并测试它是否存在并且是否是常规文件。

RewriteCond

Try using this for your second condition:

RewriteCond %{REQUEST_FILENAME} !-f

'-f' (is regular file)
Treats the TestString as a pathname and tests if it exists and is a regular file.

RewriteCond

不再见 2024-09-02 07:57:00

尝试这些规则:

RewriteCond %{QUERY_STRING} ^width=(\d+)&height=(\d+)$
RewriteCond %{DOCUMENT_ROOT}/media/$1-%1x%2.$2 -f
RewriteRule ^media/([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9]+)$ media/$1-%1x%2.$2 [L]
RewriteCond $1 !=index.php
RewriteRule ^media/([a-zA-Z0-9_\-]+\.[a-zA-Z0-9]+)$ media/index.php?file=$1 [L,QSA]

第一个规则的第二个条件测试是否存在具有该名称的文件 (-f)。

Try these rules:

RewriteCond %{QUERY_STRING} ^width=(\d+)&height=(\d+)$
RewriteCond %{DOCUMENT_ROOT}/media/$1-%1x%2.$2 -f
RewriteRule ^media/([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9]+)$ media/$1-%1x%2.$2 [L]
RewriteCond $1 !=index.php
RewriteRule ^media/([a-zA-Z0-9_\-]+\.[a-zA-Z0-9]+)$ media/index.php?file=$1 [L,QSA]

The second condition of the first rule tests if there is a file with that name (-f).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文