繁体   English   中英

mod_rewrite以匹配不是图像的文件

[英]mod_rewrite to match files that are not images

我有以下重写规则

<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 / up的每个请求均由dl-file.php处理,以检查用户是否已登录。

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

在将规则重写为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图像就会投放。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM