You can stop certain servers or sites from hotlinking your files by editing the .htaccess file in your site's root directory. Displaying different content for specific sites is quite beneficial, you may have images hotlinked on good domains that you wish them to display but have sites illegally Hotlinking to your content that you don't want them to use.
Another reason may be a certain site is using excessive amounts of your bandwidth, such as MySpace users linking to your images. So you can serve these users either a lower quality image that's smaller in size, or a "Please Don't Hotlink" image.
There's no point blocking all outsite domains because of a few bad apples, so we can allow all except specific URL's we list.
Example: To stop hotlinking images from "badsite.com" and display an image called "nohotlink" instead, use this code in your .htaccess file:
Quote:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www.)?badsite.com/ [NC]
RewriteRule .(jpe?g|gif|bmp|png)$ images/nohotlink.$1 [NC,L]
|
Line 1 "RewriteEngine On" tells Apache it's got some directives to follow.
Line 2 Tells Apache to act when your site is being accessed from either
http://badsite.com or
www.badsite.com and the [NC] says hey "No Case" or not case sensitive.
Line 3 says if Badsite.com requests a JPG, GIF, BMP or PNG file we should serve them nohotlink in your images folder.
Now, you'll notice it's written as nohotlink.$1 which means if they request whatever.jpg they will be served /images/nohotlink.jpg or if they request anything.gif they will be served /images/nohotlink.gif
Why like this? Well it's not real effective serving an alternate Filetype such as the remote server requests a .gif and you try and serve a .jpg
So doing it this way is 100% effective, however it means you will need to create a "nohotlink" image in JPG, GIF, BMP and PNG filetypes and store them in your images folder.
This directive not only works with images, it works for other file types such as CSS, Zip, Rar etc. You will just need to add the Filetype to line 4. So if we wanted to do Images and Zip files we would just alter it to:
(png|zip|gif|bmp|jpe?g)
And when your Zip files are hotlinked from the bad domain, they will receive in this case the yourdomain.com/images/nohotlink.zip
What you can do here is, open Notepad and write a simple message such as "Unfortunately this file was Hotlinked from my site, however if you would like to download it come visit us at YourDomain.com"
Save the text file as "download-info.txt" or something similar, Zip it and upload it to your site with the name nohotlink.zip
Now whenever any Zip files from your site are downloaded from Bad Domains hotlinks, their users will most likely come to your site giving you back the traffic you deserve and you might get some regular visitors out of it who Bookmark your site.
The whole thing will only cost you a lousy 4kb or so of bandwidth, and provide you with some great advertising. That will teach Bad Site to Hotlink
Blocking Multiple Domains
Ok, what if i've got a couple of Bad Domains i want to stop Hotlinking?
If you still want good sites to be able to Hotlink content, however we have a couple of bad domains we want to serve different content to the solution is easy and very similar to the above. If we have Badsite.com and Evilsite.com who are leeching our content we simply do the following:
This is similar to the single site example, the ,OR] section just tells Apache to check if the remote link is coming from Badsite.com "or" Evilsite.com and if either is requesting a file remotely to serve the "nohotlink" alternative. If the remote site is neither, Apache will serve the original content.
Simple huh?
If you want to add several sites, just copy the badsite.com line so every line has the [NC,OR] and the final Badsite line just has the [NC] enter.
To allow hotlinking images only if the REFERER is from a specific directory from your domain:
Quote:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/dir/ [NC
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .(jpe?g|gif|bmp|png)$ images/nohotlink.jpg [L]
|
To stop hotlinking images from specific outside domains only, named badsite.net and badsite.com:
Stop Sites Scraping your RSS Feed
Alot of people have been asking how to stop people scraping their RSS feeds. Well you can do a neat trick with .htaccess to do just that. This won't stop people en masse, but once you have identified a site scraping your feed you can redirect their request elsewhere to nip it in the bud.
First you need to go to command prompt on your PC and type: ping rssthief.com
Obviously replace rssthief.com with their domain name, and the IP will be output so jot it down. Next add the following rule to your .htaccess file.
Replace the 123.123.123.123 with their sites IP you got from command prompt. Now one sneaky move you can pull is add the Rss Thiefs own feed URL to the second line.
What this will do is, each time their IP pulls a request for your feed your server will redirect their request to their own feed which will pull content they have already published from their own blog and repost it resulting in them publishing their own duplicate content.
Yeah i know sneaky but hey! You could also add the feed of a porn site to the last line (with the owners permission of course) which will throw adult content on the RSS Thiefs site.. Not a good look if they are running a blog on non-fiction books.
This should help in blocking sites in .htaccess
Enjoy!