mod_rewrite 匹配非图像文件

发布于 2024-12-28 13:39:03 字数 507 浏览 1 评论 0原文

我有以下重写规则

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

,其中对 wp-content/uploads 的每个请求都由 dl-file.php 处理,以检查用户是否已登录。

我仍然需要具有该功能,但授予对图像的免费访问权限(png|) jpg|gif|jpeg) 我有点迷失了,我不知道如何修改重写规则来检查任何不是图像的文件。

I have the following rewrite rules

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Where every request to the wp-content/uploads its handled by dl-file.php in order to check if user its logged in.

I need to still have that functionality but grant free access to images (png|jpg|gif|jpeg)
Im a bit lost i dont know how to modify the rewrite rule to check any file that its no an image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅沫记忆 2025-01-04 13:39:03

在重写 dl-file.php 的规则之前,您可以插入更具体的图像规则:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(wp-content/uploads/.*\.png)$ $1 [L]
RewriteRule ^(wp-content/uploads/.*\.jpg)$ $1 [L]
RewriteRule ^(wp-content/uploads/.*\.gif)$ $1 [L]
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]

这三个规则中的每一个都具有相同的结构,我将解释第一个:
匹配上传中以 .png 结尾的任何内容
使用括号将整个路径保存到变量 $1 中。

规则的右侧给出了这个变量,所以如果你看
在一个特定的 url 中,这可以归结为:

Rewrite wp-content/uploads/some.png wp-content/uploads/some.png [L]

末尾的 L 告诉重写引擎这是要应用的最后一条规则。
因此,如果匹配,重写过程将在此结束,您的 png 图像将被提供。

before the rule that rewrites to dl-file.php you could insert more specific rules for images:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(wp-content/uploads/.*\.png)$ $1 [L]
RewriteRule ^(wp-content/uploads/.*\.jpg)$ $1 [L]
RewriteRule ^(wp-content/uploads/.*\.gif)$ $1 [L]
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]

Each of the three rules has the same structure, I'll explain the first one:
match anything in uploads that ends in .png
with the brackets you save the whole path into a variable $1.

the right hand side of the rule gives this variable, so if you look
at one specific url this boils down to:

Rewrite wp-content/uploads/some.png wp-content/uploads/some.png [L]

the L at the end tells the rewrite engine that this is the last rule to apply.
so if it matches, the rewriting process ends here, your png-image is served.

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