LAMP:将文件下载请求重定向到php页面以计算文件下载

Currently I have a client application which downloads files directly from my WEB server which is running on a LAMP server. File adresses are like this: http://example.com/application/images/*.jpg.

I'm going to count number of times file downloaded; However I cannot change client applications so they can send their request to other addresses or page.

I guess there are some techniques based on URL-rewriting so that I can convert request in form like http://example.com/application/images/xyz.jpg to something like http://example.com/application/counter.php?file=xyz.jpg. This way I can handle file downloads and count them using techniques suggested in this answer.

So I want to know how should I setup .htaccess file or use mod-rewrtie to redirect my requests to counter.php page?

My current .htaccess file looks like this:

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag  log_errors on
# AddHandler application/x-httpd-php54 .php .php5 .php4 .php3
Options -Indexes
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

P.S. My web server is running on a shared host so I don't have access to everything on this server but I can do task like adding/editing .htaccess files.

You can use these rule in your site root .htaccess:

RewriteEngine On

RewriteRule ^(application)/images/(xyz\.jpg)$ $1/counter.php?file=$2 [QSA,L,NE]

Then inside counter.php keep a count and then load jpg image using $_GET['file'] with correct MIME-TYPE.