简体   繁体   English

Apache重写忽略图像以获取PHP中的处理权限

[英]Apache rewrite ignore images for processing permissions in PHP

I am noob to REGEX and have the following code in .htaccess 我对REGEX不熟悉,并且在.htaccess中有以下代码

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

And the content of download.php file is 并且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();
}

It works great and what it does is: 它很棒,它的作用是:

  1. Triggers verification whenever the files in "uploads" directory is served by Apache, specially for direct links. 只要Apache为“上载”目录中的文件提供服务(特别是直接链接),就会触发验证。
  2. If it's a member (logged in user) serve all the files 如果是成员(已登录用户),则提供所有文件
  3. If it's not a member, only allow images, but deny access to any other files 如果它不是成员,则仅允许图像,但拒绝访问任何其他文件

But I think there is too much overhead in the download.php script to verify for image extenions each and every time, so I am trying to ignore the images directly from .htaccess rewrite rule. 但是我认为download.php脚本中的每次检查开销都太大,因此我试图直接从.htaccess重写规则中忽略这些图像。 Looks like I have to change the following code 看起来我必须更改以下代码

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

to skip the images before passing to download.php, but don't know how. 在下载到download.php之前跳过图像,但不知道如何。

Thank you! 谢谢!

Just add a condition that excludes those extensions: 只需添加排除那些扩展的条件即可:

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|bmp)$ [NC]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^uploads/(.*)$ download.php?file=$1 [QSA,L]

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

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