1. Assume that the host domain name allowed to link to the image is: www.test.com
2. Modify httpd.conf
SetEnvIfNoCase Referer "^http://www.test.com/" local_ref=1
<FilesMatch ".(gif|jpg)">
Order Allow, Deny
Allow from env=local_ref
</FilesMatch>
This simple application can not only solve the problem of hotlinking of pictures, but with slight modifications can also prevent the problem of hotlinking of any files.
When using the above method to link images from a non-specified host, the image will not be displayed. If you want to display a "hot link prohibited" image, we can use mod_rewrite to achieve this.
First, when installing apache, add the --enable-rewrite parameter to load the mod_rewrite module.
Assuming that the "hot linking prohibited" picture is abc.gif, we can configure it in httpd.conf like this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://( www.)?test.com /.*$ [NC]
RewriteRule .(gif|jpg)$ http://www.163.com/404.htm";//The address returned by the hot link
$okaysites = array(" http://www.163.com/","http://163.com "); //Whitelist
$ADMIN[url_1] = " http://www.163.com/download/";//Download location 1
$ADMIN[url_2] = "";//Download location 2, and so on
$reffer = $HTTP_REFERER;
if($reffer) {
$yes = 0;
while(list($domain, $subarray) = each($okaysites)) {
if (ereg($subarray,"$reffer")) {
$yes = 1;
}
}
$theu = "url"."_"."$site";
if ($ADMIN[$theu] AND $yes == 1) {
header("Location: $ADMIN[$theu]/$file");
} else {
header("Location: $ADMIN[defaulturl]");
}
} else {
header("Location: $ADMIN[defaulturl]");
}
?>
Usage: Save the above code as dao4.php