Forum Moderators: phranque

Message Too Old, No Replies

A harder hotlinking challenge in .htaaccess...

Redirecting to a URL based on filename they tried to access

         

Sovi3t

3:04 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Hi all,

I've been having aproblem with people hotlinking to the zip files hosted on my server. My bandwidth is limited, and google is paying less and less. To fix this, I would like visitors to be redirected to a download page when accessing a zip on my server that they were brought to from a different site.

So here's a sample scenario of what I WANT to happen:

1) A user goes to example.com (NOT my site) and tries to download a file on my server, FILE_NAME_HERE.zip (FILE_NAME_HERE can be any one of about 150 files).

2) Instead of directly downloading it, they are brought to www.mysitenamehere.com/download.php?file=FILE_NAME_HERE, where it displays an ad and starts the download within a few seconds.

3) They download the file, everything is fine and dandy. I get paid for the bandwidth (in the form of showing google ads) the hotlinking site would have stolen had I not put in protection.

Any suggestions or help is greatly appreciated!

Marino

11:01 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Hello,

This should work :

RewriteCond ([^/]+)\.zip$
RewriteRule ^.*$ www.mysitenamehere.com/download.php?file=%1 [R,L]

%1 will be replaced by the first matching sub-pattern of the RewriteCond ([^/]+), ie the file name without the extension.

Sovi3t

8:42 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



Thanks a lot Marino, i'll try it out!

Marino

6:58 am on Feb 4, 2005 (gmt 0)

10+ Year Member



Sorry, I've been a bit too fast writing my previous post :

RewriteCond %{SCRIPT_FILENAME} ([^/]+)\.zip$
RewriteRule ^.*$ www.mysitenamehere.com/download.php?file=%1 [R,L]

Guess this will work better. Well, this time, the syntax is correct...

Sovi3t

8:40 pm on Feb 5, 2005 (gmt 0)

10+ Year Member



Wow! This works great!

The only problem is that I have to allow blank referrers due to firewall problems, and sites can do a JS popup in IE to the file (no refferer). But there is no way to fix this, because I don't want some firewall users to have problems downloading.

I was wondering if you could please exaplin some on it to me, especially the %1 part. It's only in the code once and it's automatically set the the filename.

Thanks once again!

jdMorgan

9:00 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can shorten that code and speed it up a bit:

RewriteRule ([^/.]+)\.zip$ www.mysitenamehere.com/download.php?file=$1 [R,L]

For information on $1 and %1 back-references, see the Apache mod_rewrite documentation cited in our forum charter [webmasterworld.com]. The mod_rewrite documentation describes back-references in the RewriteCond and RewriteRule sections.

Jim