Forum Moderators: phranque

Message Too Old, No Replies

Hotlinking Downloads

Can you help me with .htaccess?

         

trillianjedi

5:39 pm on Feb 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,
Could anyone help me out with this one? I don't normally do this stuff as my "business" partner does all the web server related code and I get to play with the pretty pictures in photoshop and write stuff!

But he's away, and we've got a deluge of people hotlinking the above-mentioned pretty pictures.

I can do this part:-

RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif¦jpg)$ [mydomain.com...] [R,L]

But in looking at the "offenders" sites, they're also hotlinking all our downloads, on our bandwidth! Cheeky devils.

Is there anything I can add to that code to point the user to our parent download page, rather than the actual file?

All downloads are .zip without exception. So I guess I need something like the above that points a .zip to a .php page if it hasn't come from our own domain.

eg:-

They click on www.mydomain.com/downloads/widgets.zip which is hotlinked from someone elses website

and instead they get:-

www.mydomain.com/downloads/index.php

I really don't know what I'm doing here, so if you can make it simple, I'd be really grateful!

Would it be as simple as:-

RewriteRule \.(zip)$ [mydomain.com...] [R,L]

?

Thanks,

TJ

jdMorgan

9:15 pm on Feb 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



TJ,

Instead of your first ruleset, I'd suggest:


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.[b]com [NC][/b]
RewriteRule \.(gif¦jpg)$ [b]/dont[/b]_hot_link_please.gif [b][L][/b]

since the code you posted requires the cooperation of the client browser (or bot) in order to work, and requires an additional HTTP request, thus loading your server. The above version simply substitutes the file used to provide the requested image - completely inside the server.

For maximum compatibility, I'd suggest you create a jpg version of dont_hot_link_please.gif, and then use this rule:


RewriteRule \.(gif¦jpg)$ /dont_hot_link_please.$1 [L]

Each hotlinked request is then rewritten to an alternate image of the same filetype requested.

---

Question 2... Bad news: No, you cannot redirect from a zip file to an html or php page - The browser may not know how to handle the sudden and unexpected MIME-type change. Instead, simply return a 403-Forbidden response:


RewriteRule \.zip$ - [F]

(Use the same RewriteConds as above.)

You could also create a zero-length zip file, and rewrite hotlink requests to serve that file in a manner similar to the image file rewrite, but I believe strongly in keeping things simple (and robust).

Jim

trillianjedi

9:57 pm on Feb 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim, that's great.

I'll follow your advice and keep it simple.

Thanks also for the revised code.

TJ