Forum Moderators: phranque

Message Too Old, No Replies

Help with .htaccess hotlinking

My code doesnt work

         

zkac050

2:32 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



Hello, fello webmaster, i got this code, from one of the forum, and applied it to my site, but the thing is, it doesnt work, to stop people hotlinking, as i can still access pictures from a test site i created.
Can someone help me please, the file was placed in the root directory.

RewriteEngine on

RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example\.co\.uk [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example\.co\.uk [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example2\.co\.uk [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example2\.co\.uk [NC]
RewriteRule \.(gif¦jpg¦jpe?g¦bmp)$ - [F,NC,L]

[edited by: jdMorgan at 5:18 pm (utc) on Jan. 20, 2004]
[edit reason] Removed specifics per TOS [/edit]

GraphicsMuse

3:38 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



I'm in the same boat and I've tried to make this work for the past week and I'm going nuts. This is the entire content of my htaccess file:


ErrorDocument 404 /404error.html
Options -Indexes
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example.com [NC]
RewriteRule \.(gif¦jpe?g)$ /loser.$1 [L]

Please let me know what I'm doing wrong! Thank you so much for your help.

[edited by: jdMorgan at 5:18 pm (utc) on Jan. 20, 2004]
[edit reason] Removed specifics per TOS [/edit]

zkac050

3:41 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



yeah, i dunno, it works for my friends, but it doesnt work for me............aaarrgh!
He has the same code as well, and it works for him.

jdMorgan

5:38 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



zkac050 and GraphicsMuse,

Welcome to WebmasterWorld [webmasterworld.com]!

Many server configurations will require you to have Options +FollowSymLinks before any mod_rewrite directives. On others it is not needed, and on still others it can cause a 500-Server error. You must test to find out which applies.

You must have "RewriteEngine on" before any other directive processed by mod_rewrite.


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.co\.uk [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example2\.co\.uk [NC]
RewriteRule \.(gif¦jpg¦jpe?g¦bmp)$ - [NC,F]

Or an alternative rule:

RewriteRule \.(gif¦jpe?g)$ /loser.$1 [NC,L]

You must edit the code above and change the broken pipe "¦" characters to solid pipes characters - usually SHIFT - \ on a standard (U.S.) keyboard.

The [L] flag used with [F] is redundant; The combinations shown above are sufficient.

When testing, be sure to flush your browser cache (Temporary Internet Files) between requests. Also, use Shift-Reload or Control-Reload (whichever applies to your browser) to force a complete page reload from the server. Otherwise, if the image is cached, it will not be requested from your server. And if it's not requested from your server, then your code can have no effect.

Both of your code samples allow access using a blank referrer. This is almost always required to avoid massive problems with visitors who connect (knowingly or unknowingly) through corporate or ISP firewalls, caches and/or proxies which often block rererrer information. However, it will allow direct type-in-the-address-bar and right-click browser access, and there's nothing that can be done about that without significantly more complicated measures being taken.

The subjects of hotlinking and blank referrers have been much-discussed here. For more information, try a site search for "hotlinking" and "unreliable HTTP_REFERER" and similar phrases.

Jim

decdim

6:06 pm on Jan 20, 2004 (gmt 0)



Hey Jim,

Would there be any effect on the fact they didn't put a <space> between these:

REFERER} and!

--
Note: For some reason, the edit of the post doesn't produce a space between }! on the forum.

GraphicsMuse

6:24 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



Thanks to both Jim and decdim for trying to help us. This is very frustrating to show Code Dummies like me how to do this ;o)

This is what my htaccess file looks like now:


ErrorDocument 404 /404error.html
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?graphicsmuse\.com [NC]
RewriteRule \.(gif¦jpg¦jpe?g¦bmp)$ - [NC,F]

Unfortunately, it still does not work and, for the life of me, I can't figure out why. My domain host does support mod_rewrite and I've uploaded it as an ASCII file and it's CHMOD as 644 or -rw-r--r-- I've also made sure the ¦ are right.

It's in my root directory where my index.php file is, in case that makes any difference. Should I place it instead in the folder that contains the images I do not wish to be hotlinked to?

Please, if anyone can figure this out for me, I'd be very grateful! Thank you!

zkac050

10:04 am on Jan 21, 2004 (gmt 0)

10+ Year Member



thanks alot all of you, you guys are great, I am gonna try them out now

zkac050

10:08 am on Jan 21, 2004 (gmt 0)

10+ Year Member



ah man, it does not work for me also

my file now looks like
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.co\.uk [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example2\.co\.uk [NC]
RewriteRule \.(gif¦jpg¦jpe?g¦bmp)$ - [NC,F]

[edited by: jdMorgan at 6:10 am (utc) on Jan. 27, 2005]
[edit reason] Removed specifics per TOS. [/edit]

jdMorgan

6:12 pm on Jan 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before we go any further, please tell me that you are flushing your browser cache (temporary internet files) before every test, and that you are sure that the files are being loaded from your server, and not from your browser cache or your ISP cache. If you can't flush the caches, then change the image name every time you test - force the request to go all the way to your server where your htaccess code can have an effect. The easiest way to do that is to append a nonsense query string to the image URL, e.g. http://www.example.com/images/image001.jpg?test=1 ... test=99

Also, the code you are using allows blank referrers (as it must to avoid major customer-service complaints) and therefore, direct-type-in image URLs will never be blocked, so be aware of this during testing.

If the above does not help, then perhaps your server does not support mod_rewrite at all. :(

Jim