简体   繁体   English

Apache2 PHP - 如何使用 .htaccess 重写直接下载链接

[英]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:例子:

  1. Access this Direct link: https://exampledomain.com/files/file.pdf --> this does not get logged.访问此直接链接: https : //exampledomain.com/files/file.pdf --> 这不会被记录。 File transfer starts.文件传输开始。

  2. 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.

相关问题 如何使用 AllowOverride 指令允许 .htaccess 文件覆盖 php_value (Apache2) - How do I use the AllowOverride directive to permit .htaccess files to override php_value (Apache2) 如何配置Apache(.htaccess?)使用mod_rewrite将虚拟路径传递给PHP文件? - How do I configure Apache (.htaccess?) to use mod_rewrite to pass virtual path to PHP file? apache2上的符号链接和htaccess - symbolic links and htaccess on apache2 如何用`.htaccess`重写URL,不允许直接链接? - How to rewrite URL with `.htaccess`, which disallows direct links? .htaccess 使用 REQUEST_URI 返回 500 错误代码。 如何解决? Apache2 + PHP - .htaccess to use REQUEST_URI returns 500 error code. How to fix it? Apache2 + PHP Apache 2在Phalcon PHP Framework中重写 - Apache2 Rewrite in Phalcon PHP Framework 如何让 LiteSpeed 像 apache2 那样评估 .htaccess 中的所有重写规则? - How to get LiteSpeed to evaluate all rewrite rules in .htaccess as apache2 does? PHP / Apache:使用.htaccess重写规则 - PHP/Apache: Rewrite rules with .htaccess PHP可以比Apache直接文件访问或.htaccess中mod_rewrite的RewriteRule更快吗? - Could PHP be faster than Apache direct file access or RewriteRule of mod_rewrite in .htaccess? htaccess mod_rewrite & apache2 不重写正斜杠 - htaccess mod_rewrite & apache2 not rewriting forward slash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM