.htaccess RewriteRule,将请求发送到 PHP 脚本

.htaccess RewriteRule,将请求发送到 PHP 脚本

我正在尝试保护目录 /secret 中用户上传的文件。我创建了一个 htaccess 文件并将其放入该目录中。

RewriteBase /secret/
RewriteCond %{REQUEST_URI} !^download.php.*$
RewriteRule .* download.php [QSA,L] #send request to php script, that script sends out the file if user is authenticated

只要所有文件都在 /secret 目录中,这种方法就很好用。但当有子目录时就会出现问题,例如 /secret/subfolder/document.txt。现在,当我使用浏览器请求此文件时,Apache 会发送该文件。它不会被重写规则重写,任何人都可以下载该文件。

有什么技巧可以让我重定向 /secret 文件夹内的所有请求以通过 download.php 脚本?这样 /secret/file.txt 和 /secret/subfolder/document.txt 等都可以正常工作。我只能使用 php 进行身份验证,并且我希望保持目录结构不变,因为这些文件用作各种会员专用页面上的链接。

谨致问候,Jdoen

答案1

RewriteBase /secret/
RewriteCond %{REQUEST_URI} !^download.php.*$
RewriteRule ^(.*)$ download.php [QSA,L] #send request to php script, that script sends out the file if user is authenticated

如果这不起作用,请尝试删除 ^(.*)$ 前面的 ^

相关内容