Forum Moderators: phranque
My code is working (many thanks to JDMorgan).. but I have one problem that had me perplexed, until I finally changed one line of code at a time to figure out why the hotlink protection would work sometimes and not others.
What I had tried to do was take my old hotlink .jpeg image and add some text (thru adobe photoshop elements) and created a separate .jpg image, .bmp image, .gif image.
initially, I thought it was my hotlink code that was wrong, but then I figured out that the code worked with my old .jpeg image, but not with any of my new images. so, I went back to my old .jpeg image and it works for .gif, and .jpg images on my site.
I was wondering why my new images don't work; why the browser won't substitute them.
is there anything that comes to mind? I can give examples.. but I notice that specific details about sites are not allowed?
so, it is not the image. It is the way I have .htaccess set up. ack.. back to the drawing board.
I will have to figure out why the examples I saw here work and my htaccess doesn't.
this is my htaccess:
AddType 'text/html; charset=UTF-8' html
#
ErrorDocument 404 /404.html
#
Options +FollowSymLinks
#
RewriteEngine on
# prevent hotlinking
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain\.com [NC]
RewriteRule .*\.(gif¦jpg¦png¦bmp)$ /i/graphictheft.jpeg [L]
#
# Redirect anything that's NOT www.mydomain.com to www.mydomain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteRule (.*) [mydomain.com...] [L,R=301]
1) Client did not send HTTP referer, and is therefore allowed to fetch the correct/original image.
2) When testing flush your browser cache before doing a hotlink request. Otherwise, your browser may have cached the image, and if so, will show you the cached version, and not try to fetch the image from your server.
To be clear, images are cached by URL. When you put hotlink code in place, you really have one URL that points to two possible images -- the original/correct one, and the replacement to be served to hotlink requests. Therefore, you must flush your cache while testing to avoid confusion.
Anti-hotlinking methods based on the HTTP Referer header are unreliable, but simple. They work often enough to discourage hotlinkers, and are simple to implement. When referrer-based anti-hotlinking does work, it makes the hotlinker's site look broken, or displays an alternate image (possibly with your watermark or URL, or an overlaid message directing the viewer to your site, or --if you prefer fun over professionalism-- a naughty or disgusting picture). In most cases, that's good enough to get your image's link removed.
Standard anti-hotlinking code won't work for type-in URLs, some types of JavaScript image requests, media player image requests, and requests from users behind ISP or corporate caching proxies. But like I said, it's easy to implement, and usually works well enough.
Jim
In this case, it is like it is looping. but I don't see why.
anyway, I'm trying some other stuff. I put up a public folder, and will put the hotlink replacement images in this folder. Will post if this works.
here is the new htaccess code:
RewriteEngine on
# prevent hotlinking
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER}!^http://(www\.)?owenfoundation\.com [NC]
RewriteCond %{REQUEST_URI}!^/i/public/
RewriteRule .*\.(jpeg¦gif¦jpg¦png¦bmp)$ /i/public/graphictheft.jpeg [L]
This works also.
I'm wondering if the redirect in the code after my hotlink checks was creating the loop somehow? Although, I would think that the original hotlink replacement image would not trigger the redirect because it would be the correct domain. but.. I can't think of another reason that it would loop. Doesn't mean there isn't a reason.. :\
anyway, here is the code that works:
AddType 'text/html; charset=UTF-8' html
#
ErrorDocument 404 /404.html
#
Options +FollowSymLinks
#
RewriteEngine on
# prevent hotlinking
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mydomain\.com [NC]
RewriteCond %{REQUEST_URI}!^/i/public/
RewriteRule \.(jpeg¦gif¦jpg¦png¦bmp)$ /i/public/graphictheft.$1 [NC,L]
#
# Redirect anything that's NOT www.mydomain.com to www.mydomain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteRule (.*) [mydomain.com...] [L,R=301]
# prevent hotlinking
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !graphictheft\.
RewriteRule \.(jpe?g¦gif¦png¦bmp)$ /i/public/graphictheft.$1 [L]
Jim