[英]Apache2 PHP - How do I use .htaccess to rewrite direct download links
I have a directory of files which are downloaded by users package managers using the direct link to the file.我有一个文件目录,由用户包管理器使用文件的直接链接下载。 I'm trying to set up file logging, so I can get statistics on the downloads.
我正在尝试设置文件日志记录,以便获取有关下载的统计信息。 I'm using this script I found on GitHub: https://github.com/iNamik/PHP-Download-Tracker
我正在使用我在 GitHub 上找到的这个脚本: https : //github.com/iNamik/PHP-Download-Tracker
I'm using the above script that consists of a files directory, a log directory, and an index.php which lists the files to download.我正在使用上面的脚本,它由一个文件目录、一个日志目录和一个列出要下载的文件的 index.php 组成。
Index.php lists all files in the download directory. Index.php 列出了下载目录中的所有文件。 This file can be renamed to anything, ie download.php
这个文件可以重命名为任何东西,即download.php
If I use the index.php and click on the file I want to download, it logs the information and downloads it.如果我使用 index.php 并单击我想下载的文件,它会记录信息并下载它。
If I use the direct link to the file (/downloadfolder/file.exe) the index.php is bypassed and nothing is logged.如果我使用文件 (/downloadfolder/file.exe) 的直接链接,则绕过 index.php 并且不会记录任何内容。
Is it possible to use something like mod_rewrite in Apache, to add download.php?file= before the file name in the direct link?是否可以在 Apache 中使用类似 mod_rewrite 的东西,在直接链接中的文件名之前添加 download.php?file= ?
Example:例子:
Access this Direct link: https://exampledomain.com/files/file.pdf --> this does not get logged.访问此直接链接: https : //exampledomain.com/files/file.pdf --> 这不会被记录。 File transfer starts.
文件传输开始。
Have it be automatically rewritten to: https://exampldomain.com/files/download.php?file=file.pdf --> This does get logged.让它自动重写为: https://exampldomain.com/files/download.php?file=file.pdf --> 这确实被记录了。 And the file transfer starts
然后文件传输开始
This will redirect anything that doesn't exist to index.php这会将任何不存在的内容重定向到 index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]
After some hair pulling and testing using a htaccess tester, this is what I came up with.在使用 htaccess 测试仪进行一些头发拉扯和测试之后,这就是我想出的。
My download.php is inside the files directory.我的 download.php 位于 files 目录中。
RewriteEngine On
RewriteBase /files/
RewriteRule ^(.+(deb|DEB))$ download.php?file=$1 [L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.