Recently, I was checking out my traffic logs and I noticed that there was another site that was hotlinking some of my images (actually, this blogger had copy/pasted the entire content of my posts) and, because I don’t like people to steal my bandwidth (or money), I edited my existing .htaccess file to prevent this theft!
There are thousands of sites in the Internet that explain what an .htaccess file does, so I won’t get into it here. If you want to learn more, perform a Google search for .htaccess.
How to Create an .htaccess file
- Create a new file in favorite text editor and save it, without a name, with the extension: .htaccess

-
# BEGIN WordPress
-
RewriteEngine On
-
RewriteBase /
-
RewriteCond %{REQUEST_FILENAME} !-f
-
RewriteCond %{REQUEST_FILENAME} !-d
-
RewriteRule . /index.php [L]
-
# END WordPress
- Now, in order to prevent hotlinking, enter the following code:
-
Options +FollowSymlinks
-
# Protect Hotlinking
-
RewriteEngine On
-
RewriteCond %{HTTP_REFERER} !^http://(.+.)?YOURURL.com/ [NC]
-
RewriteCond %{HTTP_REFERER} !^$
-
RewriteRule .*.(jpe?g|gif|bmp|png|zip|rar)$ http://YOURURL.com/PATH/TO/REPLACMENT/IMAGE [L]
-
If you are a WordPress user and have turned on mod_rewrite for permalinks, then WordPress should have inserted the following code in the .htaccess file that is on your server:
Explanation of Code
Here are the variables that you will need to change in order to protect your files:
Line 4: You will need to replace YOURURL with your site’s URL. As you can see, the code already has “http” so only need to enter the URL. For example, for my site, I would have entered:
samanathon.com
Line 6: This line specifies the file types to be blocked from hotlinking and allows you to specify a file that will be replaced when hotlinking occurs. If someone hotlinks a file, it will be replace with the file that you specify here.
An issue that you need to be aware of is that we’ve just told the server to block all hotlinks with those extensions, so your replacement image needs to have an extension that is not listed on this line of code. If your replacement file is an image (which it should be), simply give the file a different extension. For example, my replacement images is an .png file, this is the file:
http://samanathon.com/images/hotlinking.pn
Options
Google Cache/Image Search/Reader
Google does create a cached version of your pages when the search spiders index a page. You can choose to allow the cache server permission to like to your images. This is comes in handy when someone is viewing a cached version of your site. All you need to do is create a like and add “google.com” as the URL, similar to the line with your site’s URL:
-
Options +FollowSymlinks
-
# Protect Hotlinking
-
RewriteEngine On
-
RewriteCond %{HTTP_REFERER} !^http://(.+.)?YOURURL.com/ [NC]
-
RewriteCond %{HTTP_REFERER} !^http://(www.)?google.com/.*$ [NC]
-
RewriteCond %{HTTP_REFERER} !^$
-
RewriteRule .*.(jpe?g|gif|bmp|png|zip|rar)$ http://YOURURL.com/PATH/TO/REPLACMENT/IMAGE [L]
Redirects
There has been much debate on the use of “www” in your URL. I prefer not to use it and I have my server redirect a request of www.samanathon.com to samanathon.com. This is how you can achieve this redirect, enter the following into the same .htaccess file:
-
# Redirect
-
RewriteEngine on
-
RewriteCond %{HTTP_HOST} ^(www.YOURURL.com)(:80)? [NC]
-
RewriteRule ^(.*) http://YOURURL.com/$1 [R=301,L]
Simply replace YOURURL with your site’s URL.
Final Code
If you’ve used all of the code that I’ve suggested, this is what your final file should look like:
-
# BEGIN WordPress
-
RewriteEngine On
-
RewriteBase /
-
RewriteCond %{REQUEST_FILENAME} !-f
-
RewriteCond %{REQUEST_FILENAME} !-d
-
RewriteRule . /index.php [L]
-
# END WordPress
-
-
Options +FollowSymlinks
-
# Protect Hotlinking
-
RewriteEngine On
-
RewriteCond %{HTTP_REFERER} !^http://(.+.)?YOURURL.com/ [NC]
-
RewriteCond %{HTTP_REFERER} !^$
-
RewriteRule .*.(jpe?g|gif|bmp|png|zip|rar)$ http://YOURURL.com/PATH/TO/REPLACMENT/IMAGE [L]
-
-
# Redirect
-
RewriteEngine on
-
RewriteCond %{HTTP_HOST} ^(www.YOURURL.com)(:80)? [NC]
-
RewriteRule ^(.*) http://YOURURL.com/$1 [R=301,L]
-
order deny,allow
Upload
This file nees to be in your site’s root and not in a folder. Navigate to your (or use an FTP client to access) your site and place the .htaccess file in the site’s root.

Just out of curiosity, how do you feel about somebody using your images if it isn’t wholesale theft? Would it be okay if someone used your images and linked them to the blog? Quote you with a link and trackback? Should they e-mail you first?
I’m not asking for permission, just curious as to your policy?
Personally, I spend a lot of time and effort to capture and tweak my images to strike a balance between quality and bandwidth utilization. I would prefer that a blogger create their own images instead of “borrowing” mine.
Also, I get really irritated when someone copy/pastes the entire contents of my posts. Again, a lot of effort goes into my blog.
interesting post, hotlinking mean steal our bandwidth , it also mean steal our money,:)
Worked perfectly on one of my blog.
But,
It lead to another problem.
I created a replacement image of 500px 200px dimension.
Copied image of 600px 800px from the target blog to another blog.
It displayed the replacement image but in the dimension of 600-800px (stretched), making it hard to see the warning message.
Is there anyway to specify the dimension of the replacement image to be displayed on othersites via .htaccess ?
Thank you.
Babaji M P´s last blog ..The Internet’s Most Hard Marketing Scheme
@babaji – i think the other blog will interpret the image as the original one, not the replacement so it will keep the width/height of the original one
i dont think you can override that aspect
Saman,
I didn’t feel that you answered Sid’s question. I frequently quote a snippet from somewhere else, and link to the original article using a graphic from the destination. If anything I feel that I am the one doing the recipient of the backlink the favour. I am promoting their site, and adding authority in Google’s eyes.
nick´s last blog ..Summary of Seven Scams Students Suffer Searching for Studios
I used the URL to the post that actually had the image in it, so i could benefit from the additional traffic.
This HowTo rocks! it worked for me. thanks for posting it!
ClickPimpsta´s last blog ..EBOOT2ISO v1.1 released
I find using WordPress plug-ins more effective.
Kalvster´s last blog ..Free Ubuntu VPN with HotSpot Shield (My Attempt)
That’s a comprehensive intro for beginners who don’t know htaccess rules. Thanks.
Sleep disorders´s last blog ..The Solfeggio Frequencies