Forum Moderators: phranque

Message Too Old, No Replies

Preventing hotlinking AND increasing link juice

Would this work in .htaccess?

         

WebWalla

3:04 pm on Feb 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,
Since many of my images are hotlinked from external websites, I'd like to prevent this practice and at the same time gain some link love if the image hotlinks remain in place. The idea is to redirect to my homepage when a hotlink is detected. Would the following work in .htaccess?

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

Thanks!

JAB Creations

9:06 am on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should seriously be testing things out on a local server first. I highly recommend using the base element for images, scripts and linked files to make the same software work both locally and live regardless of the path. It may seem like a pain at first though testing locally is always better than trying something new live. XAMPP is my recommendation for a quick and easy local LAMP/WAMP server though others may have other recommendations.

Try the following code below. You could try to redirect them to the home page however since they're stealing an image from a different domain name then I don't see the point in trying to load your page where an image would be displayed. Instead I'd recommend showing an image in it's place and you could be creative or just display nothing. I've also added both an IP address exception as well as a domain name exception so if you want a search engine cache page or a friend who owns a site to show the images from your domain name you can add further exceptions. However you should have cached pages disabled to prevent content scrappers from scrapping your content from cached pages. Hope this helps. :)

- John

RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://?255.255.255.255 [NC]
RewriteRule \.(jpe?g|gif|bmp|png)$ images/punchofpunks.gif [L]

g1smd

9:32 am on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Using Referrer data is highly unreliable. Many users behind caching proxies will not be able to supply that data so will not be able to use your site. You should allow blank referrer to be valid. Google will not supply a referrer for example. Referrer can also be faked.

You should investigate using cookies.

WebWalla

9:56 am on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys. I think if I go ahead it will be with a creative image swap.

JAB Creations

10:08 am on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



g1smd is correct. In example you could use a scripting language and cookies in example to determine if the user has browsed your site or if they haven't and if they haven't then not display the image. I would recommend storing images in a database in general for a few reasons, it would be easier to handle in general though I don't know if Apache can interact with cookies?

Regardless the referrer data isn't completely unreliable and may temper the problem for the most part unless it's a rather unique scenario.

- John

jdMorgan

9:42 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the very least, requests with blank referrals must be allowed, and the infinite looping prevented:

RewriteCond %{REQUEST_URI} !^/images/no-hotlink\.gif$
RewriteCond %{HTTP_REFERER} !^(https?://((www\.)?example\.com|123\.45\.56\.89).*)?$ [NC]
RewriteRule \.(jpe?g|gif|bmp|png)$ images/no-hotlink.gif [L]

The allowance for access by IP address is only needed if you are on IP-based hosting --that is, if your site has a unique/non-shared IP address-- and if you wish to link to images using your server's IP address.

Jim