1

我是 REGEX 的菜鸟,并且在 .htaccess 中有以下代码

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^uploads/(.*)$ download.php?file=$1 [QSA,L]

而download.php文件的内容是

require_once ('users.php'); // user session 

$allowedExtensions = array(
    '.jpg',
    '.png',
    '.gif',
    '.ico',
);

$extension = substr($_GET['file'], -4);

// Requested file is not in allowed extension and is not a logged in user
If (!in_array($extension, $allowedExtensions) && !is_user()) {
    echo '<h1>Sorry, only members can download the files.</h1>';
    exit();
}

它工作得很好,它的作用是:

  1. 每当“上传”目录中的文件由 Apache 提供服务时触发验证,特别是对于直接链接。
  2. 如果它是成员(登录用户)提供所有文件
  3. 如果它不是成员,则只允许图像,但拒绝访问任何其他文件

但我认为 download.php 脚本每次验证图像扩展的开销太大,所以我试图直接从 .htaccess 重写规则中忽略图像。看来我必须更改以下代码

RewriteRule ^uploads/(.*)$ download.php?file=$1 [QSA,L]

在传递给download.php之前跳过图像,但不知道如何。

谢谢!

4

1 回答 1

0

只需添加一个排除这些扩展的条件:

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|bmp)$ [NC]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^uploads/(.*)$ download.php?file=$1 [QSA,L]
于 2013-11-10T21:56:07.037 回答