Forum Moderators: phranque

Message Too Old, No Replies

hotlink protection

         

jvillar

4:21 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



Hello all!

I'm trying to protect my web images from hotlinking!
It's my first time with linux and Apache so I have tried some examples copied from internet like this but doesn't work:

RewriteEngine On
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example\.com/ [NC
RewriteCond %{HTTP_REFERER}!^$
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ images/nohotlink.jpg [L]

My internet host provides me with .htaccess
(AllowOverride, AuthConfig, Indexes, Limit)
directives

Any idea? thanks

jdMorgan

12:11 am on Jan 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jvillar,

Welcome to WebmasterWorld!

Since you didn't say *how* it didin't work, all I can suggest is to try this:


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ /images/nohotlink.jpg [L]

Change all broken pipe "¦" characters to solid pipe characters before use.

If you get a 500-Server Error, then check your server error log file to see what the problem is.

The references cited in our forum charter [webmasterworld.com] may be useful to you as well.

Jim

jvillar

7:39 am on Jan 9, 2005 (gmt 0)

10+ Year Member



Thank you for your reply!

I tell you how doesn't work! I have been doing some tries since I posted this.

I have now realized that it's just the same effect when I insert real ".httacces" code or I insert anything else like "afjaklfjñafjla"
It's the same efect.

Before puting .httacces with any code in my img/ directory simply I can't acces to any file in this directory.

¿I wonder if theres any way to know what's happening? ¿Is there a log file for .httaccess?

Thank you

Marino

10:27 am on Jan 13, 2005 (gmt 0)

10+ Year Member



Hi,

What is your problem exactly? An "Error 500: internal server error"?

If so, here is why: your image "nohotlink.jpg" just match... your \.(jpe?g¦gif¦bmp¦png)$ regexp, so the request will loop until the "Error 500".

Just try to rename your image as "nohotlink.jPg" (with a capital "P"), so it will not match anymore.

jdMorgan

4:01 pm on Jan 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jvillar,

Marino has indeed spotted a serious flaw in the code. To fix it, simply exclude requests for the replacement image from being rewritten:


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/images/nohotlink\.jpg$
RewriteRule \.(jpe?g¦gif¦bmp¦png)$ /images/nohotlink.jpg [L]

Jim

Marino

9:45 am on Jan 14, 2005 (gmt 0)

10+ Year Member



Your solution is actually cleaner than mine.

sakkans

12:56 pm on Jan 29, 2005 (gmt 0)

10+ Year Member



hi.. i want to use hotlink protection for my zip files...

i did what you say.

for example, my link is:
[mydomain.com...]

when i write this to a forum (another site) and click... hotlink protection works well!

but.. if i write this to adress bar (about_blank page) and click "go" download is starting...

how can i make this file available for only "clicks" in my site... no outcoming servers! and no "adress bar" entry...

thanks ;)

jdMorgan

4:52 pm on Jan 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> how can i make this file available for only "clicks" in my site... no outcoming servers! and no "adress bar" entry...

You can't -- not with a simple mod_rewrite solution, anyway. The HTTP_REFERRER value is notoriously unreliable, and often not present. Therefore, the code must allow blank referrers. Otherwise, it will block many users behind corporate and ISP caching proxies (like AOL), and all users who have Norton Internet Security installed on their machines.

The proper and thorough way to implement hotlink protection is to use a script that checks for a cookie set by your site, and then serves the images (kept in an HTTP-inaccessible directory) only if the cookie is present and correct. For even more security, set the cookie only after the user has logged in.

Jim

sakkans

1:41 am on Jan 30, 2005 (gmt 0)

10+ Year Member



The proper and thorough way to implement hotlink protection is to use a script that checks for a cookie set by your site, and then serves the images (kept in an HTTP-inaccessible directory) only if the cookie is present and correct. For even more security, set the cookie only after the user has logged in.

how can i do this? i'm liek rookie :) but i want to protect my "zip" links...

please help me...

jdMorgan

1:52 am on Jan 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This subject is rather advanced, and would require hours to write up in even a cursory way. It's also not Apache-specific.

Try some searches on the web for subjects combining scripts, cookies, access control, and hotlinking. You can probably buy a script to do what you need -- at least one and maybe hundreds (not my area of expertise, so not sure). But that's what it boils down to, learn to code this yourself or pay for it. You could use PHP or PERL for the script, so if you are fluent in either of those popular languages, they're both good choices. We also have forums specifically for those languages here on WebmasterWorld. In fact, they may have already discussed this subject over there.

The short description I gave above can serve as a to-do list for the steps of the project.

Jim