Paulund

Prevent Hotlink Images With htaccess

There is a way you can prevent people hotlinking the images on your website just by adding bits of code in your htaccess.

Images

Hotlinking

Hotlinking is when people use images from another site on their own site. This is a common practice which will waste a lot of bandwidth from the site that hosts the images.

With many hosting accounts you have a limit on the amount of bandwidth you can use and if you are wasting your bandwidth on other people using your images on there site you could reach your limit quickly. If these websites get lots of traffic then the bandwidth on your server will go up and your website isn’t getting any of the visitors.

There are a few tricks you can do to prevent people from hotlinking your images but I am going to show you one way to prevent hotlinking of your images using htaccess.

Copy the following code snippet into your htaccess file to prevent hotlinking your images by redirecting all hotlinking to one image.


RewriteEngine On
#Replace ?yoursite\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yoursite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/dontstealmyimages.jpg with your NO hotlink image.
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/dontstealmyimages.jpg [L]
Rockstar Freelancer

Writen By Paul

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5.

Website: http://www.paulund.co.uk

Feedback

Anything to add?

  • Mike Thornley

    How do I test this?. I have tried and it shows the requsted image, not my default warning one, the person should see. Is that as it’s me somehow and not a geuine hotlink attempt?.

    • http://www.paulund.co.uk Paul

      Hello Mike

      To test this try the following:

      Have two sites we’ll call them Site A and Site B.

      Site A has the image stored on it’s domain, then go to Site B and use the Site A image.

      Put the prevent hotlink snippet in the htaccess file of Site A. Go to Site B where you put the image and you should see the redirected image.

      Let me know if this helps.

      Thanks

  • Mike Thornley

    Hi Paul and thanks for the reply!.

    I tested it in another way as I don’t have another domain apart from mine – I know that’s probably far from ideal.

    I dropped a simple html file into my dropbox account and ran that and it works but doesn’t show the image I specified. Either way it works and doesn’t show the image the user would want so result!. I’ll look once again at the code.

    Many thanks

    Mike

    • http://www.paulund.co.uk Paul

      Do you a dev setup? It should work on a localhost domain.

  • Mike Thornley

    It works as intended and yeah I have xammp installed. Could i have used that?. :-)

    • http://www.paulund.co.uk Paul

      Yea it just works off the http host name which would of been localhost.

  • http://www.logocontestreviews.com/99designs-review/ Henry James

    Good tips advised, its really bit useful which i am looking for to make some changes on my site’s htaccess file. Thanks for helping in such a good way.

    • http://www.paulund.co.uk Paul

      Yea it is really helpful can save your bandwidth.