Forum Moderators: phranque

Message Too Old, No Replies

Hotlink frustration

prevent Hotlinking with htaccess file

         

warmonger

3:29 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



I know this topic has been beaten to death on this forum, especially just with changes to the htaccess file. Believe me, I've spent the last 12 hours working on this issue and scouring every example in this forum and losing my mind on Apache's site. Any help is greatly appreciated.

I'm trying to implement hotlink prevention to any graphic on my domain except from my site and one other site. This is what I have in my htaccess file:


AddHandler server-parsed .htm
Options +Includes -Indexes


# make .mydomain have a www in front of it
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]


# block hotlinkers except for my site and another site
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com(/)?.*$ [NC[b],OR]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?OtherDomainIwantToAllow.com(/)?.*$ [NC][/b]
RewriteRule \.(gif¦jpg¦jpeg¦bmp)$ http://www.mydomain.com/hotlink.jpg [R,NC]

The bolded code above generates a 500 error. If I remove the bolded code I do not get the 500 error, but hotlinking is still not working. There are spaces in between the } and!^ but I can't seem to get it to post properly here. Oh, wait a minute. The good old ALT + 255 works for spaces. Nevermind. ;)

I've flushed my cache, Ctrl-Refreshed my browser, tried it on different computers, tried with different graphics, viewed my graphics from a couple of different external sites, etc. on almost every testing attempt.

I think I'll go down to the wig shop now, since I've pulled out most of my hair. Thanks for any insight you can provide!

jdMorgan

4:18 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



warmonger,

Welcome to WebmasterWorld!

The problem is likely one of an infinite loop, since the hotlinker will be redirected to hotlink,jpg, but your rule will redirect that redirected request, too. So, you need to exclude requests for your hotlink.jpg image from being redirected.

There is also a logic error in the code and some "bad form" that should be cleaned-up.


Options +FollowSymLinks
RewriteEngine on
RewriteRule %{REQUEST_URI} !^/hotlink\.jpg$
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [b][NC][/b]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?OtherDomainIwantToAllow\.com [NC]
RewriteRule \.(gif¦jpe?g¦bmp)$ /hotlink.jpg [NC]

Note: Added anti-looping exclusion, no [OR] on your second RewriteCond, deleted redundant trailing "(/)?.*$" from domain patterns (the revised code accomplishes the same thing), escaped all literal "." characters in patterns by preceding them with "\", compressed "jpeg¦jpg" to "jpe?g", and changed to an internal rewrite to "hide" the action of this code.
And as always, change all broken pipe "¦" characters to solid pipes before use.

Hopefully, that'll help you keep your hair. If not, check the references cited in our charter, and if that fails, I'm sure some of our members here are marketing hair-restoration products... ;)

Jim

warmonger

4:50 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks Jim! You should write a book on this. I bet you'd make a fortune. I wish I could sit here and pick your brain all day, as I have a few other questions. I'll save you the trouble though and swim back into deeper waters over at Apache. (Now where did I put my water wings at?)   ;)

I'll give this a try. Thank you very much for your quick assistance. Too bad I already spent $49.99 on a hair piece. :D I guess it'll grow back in time.

warmonger

5:11 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Ok, no errors and it seems to work, but with a hitch.

    1. It's not showing the hotlink.jpg in replace of the requested image.

[Edit] Spoke too soon on part of this. Was dealing with a cache issue on other images still showing up, but the hotlink.jpg is still a no show at this point. If all that it will do will display a red X or empty image placeholder instead of the hotlink.jpg, I guess I can live with that. Still, the hotlink.jpg would have been a nice feature. Thanks again, Jim! I'll send you a bottle of hair tonic as a reward.[/edit]

jdMorgan

7:51 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may be dealing with an IE bug here. IE won't reliably display images smaller than 1kB in size.
Other than that, yes, flush your cache after *any* change to *any* access-control code. The code is quite "standard" and should work as expected.

Jim