View Full Version : selective hotlink blocking
tickletrunk
11-14-2004, 03:39 AM
Hi! I have a webpage that offers free images and other graphics. I have no problem with people hotlinking my images, but I ask that people give me credit. Recently I have found some people that are not crediting my work. I would like to be able to block their specific urls from hotlinking my files. I know I can use htaccess to block an entire domain from hotlinking, but is it possible to block one specific url? (ie: freewebs.com/noncrediter) If anyone can provide any insights on this, I would greatly appreciate it! Thanks!
Philip M
11-14-2004, 09:40 AM
This will block unwanted domains from accessing your site (assuming that there are not too many of them).
<SCRIPT language = "JavaScript">
var unwantedDomains = new Array("domain1.com", "domain2.com", "domain3.com", "domain4.com");
var domainCheck = document.location.href; //retrieve the current URL of user browser
var acceptedOK = true; //set access to true by default
if (domainCheck.indexOf("http") != -1) { //if this is a http request check validity
for (r=0;r<unwantedDomains.length;r++) {
if (domainCheck.indexOf(unwantedDomains[r]) != -1) { //if a match is found
acceptedOK = false; //set access to false, and break out of loop
break;
}
}
}
if (!acceptedOK) {
alert ("Only approved domains are allowed to link \ndirectly to this file on our server! ");
history.back (-1);
}
</SCRIPT>
gsnedders
11-14-2004, 01:43 PM
Yes, but that would only block people from hotlinking to (X)HTML files, a more sensible solution would be to use an .htaccess if your server uses Apache.
Philip M
11-14-2004, 04:34 PM
If you want to stop hotlinking one way is to rename your image files
regularly and give other (improper?) images their old names. They'll soon stop!
There is a way thru .htaccess by just naming the folder containing images you don't want hotlinked... But that doesn't let you specify a URL to block, just folders you want blocked from everyone else.
The way I've handled this is whenever someone hotlinks one of my images, it pops up my website, specifically to a page saying they got (or hoisted) the image from me & here's my URL. This basically gives me credit whether the hotlinker wants to give it or not. :D
As an example, I've "hotlinked" to an image on my site. Click the link & see where it takes you.
http://family.jandjwebservices.com/photos/judy13.jpg
If no image was linked to (error debugging), this is what it shows instead:
http://family.jandjwebservices.com/showpic.php
First, you'll need to make a web page for viewing the image's origination. The important code is in green below; the rest of the page can be altered to match your website's look. Of course, change the URL of http://www.yourdomain.com/ to reflect yours. Once you've made the page below, name it "showpic.php".:
<?php
header("Content-type: text/html");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache,
must-revalidate");
header("Cache-Control: post-check=0, pre-check=0",
false);
header("Pragma: no-cache");
$pic = strip_tags( $_GET['pic'] );
if ( ! $pic ) {
die("No image specified. <a href=\"http://www.yourdomain.com/\">http://www.yourdomain.com/</a>");
}
?>
<html>
<head>
<title><?php echo($pic); ?></title>
</head>
<body>
<p align="center">
<img src="<?php echo($pic); ?>" alt="Image" />
</p>
<p align="center">
This image is being hoisted from the <em>BLAH BLAH</em> website, ©2004 All Rights Reserved.<br />
Feel free to visit our website at <a href="http://www.yourdomain.com/">http://www.yourdomain.com/</a>.
</p>
</body>
</html>
Then add this to your ".htaccess" file, MINUS the comments in red - which are for you. Change the URLs as needed. This is what redirects people to the page you've just made.:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC] <--- Name the file types you want blocked
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !yourdomain\.com [NC] <-- Name your website
RewriteCond %{HTTP_REFERER} !yourotherdomain\.com [NC] <-- Name other websites you want to give access to if any
RewriteCond %{HTTP_REFERER} !google\. [NC] <-- This is so Google can still crawl your page & cache it
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]<-- Same reason as above :)
RewriteRule (.*) /showpic.php?pic=$1<-- Enter the name/location of the web page you just made. If you followed the first step, you shouldn't have to make any changes to this.
Now all you have to do is upload it and see if it works. FYI, I got this nifty trick from ALA at http://www.alistapart.com/articles/hotlinking/ .
Hope this helps! Let me know how it works out...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.