Protect redirected file from hotlinking

254 Views Asked by At

I have a link to download a file, similar to:

http://example.com/download/file.zip

The file is forwarded to a php script using an .htaccess rule. The script does download counting, and then redirects to the actual file:

http://example.com/download/the_real_file/file.zip

I have no problem with other websites linking to download/file.zip, but the issue is that some websites link to download/the_real_file/file.zip directly, thus breaking the statistics, as the script doesn't do the download counting.

I want to prevent direct linking to download/the_real_file/file.zip.

I tried to do that with an .htaccess rule:

# don't allow sites to link to download/the_real_file/file.zip
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !example.com [NC]
RewriteRule ^download/the_real_file/file\.zip$ / [R,L]

This approach works, but it blocks linking to download/file.zip as well, as the original referrer is passed to download/the_real_file/file.zip, too.

0

There are 0 best solutions below