Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How to Prevent Image Hotlinking in Apache with .htaccess

    How to Prevent Image Hotlinking in Apache with .htaccess

    By RahulAugust 25, 20222 Mins Read

    What is Image Hot linking?

    To display an image on a website from another website. For example, you have created some stunning images on your website and another user uses those images on their website using your domain link. In this case whenever the other user’s website loads, The image is loaded from your website. This can increase the hosting cost for you.

    Advertisement

    This tutorial will help you to prevent image hotlinking on Apache using .htaccess.

    Prevent Image Hot Linking

    Create a .htaccess file under the document root of your website. Then add the following rules to the created .htaccess file. This will block all domains except example.com from hotlink images from your site.

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?example.com [NC]
    RewriteRule \.(jpe?g|png|gif|bmp|tif)$ - [NC,F,L]
    

    The above in .htaccess code will prevent other domains from linking images from your website. You can also show a blocked banner in place of the hot-linked image. To show the banner use the below rules in your .htaccess file.

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^https?://(.+\.)?example.com [NC]
    RewriteRule \.(jpe?g|png|gif|bmp|tif) http://example.com/blocked.png [R,L]
    

    Disable Image Hotlinking for Specific Domains

    Sometimes you may need to block image hotlinking from some specific domains. It can be any domain or subdomain name. Use the following example code in the server .htaccess file. This will block image hotlinking for the domains blockthis.com and baddomain.net.

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} ^https?://(.+\.)?blockthis\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} ^https?://(.+\.)?baddomain\.net [NC,OR]
    RewriteRule \.(jpe?g|png|gif|bmp|tif)$ - [NC,F,L]
    

    Conclusion

    In this tutorial, you have learned to prevent images to be hot linked by the unknown domains in the Apache server.

    Apache hotlinking htaccess Images
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    20 Basic Linux Commands for the Beginners (Recommended)

    tail Command in Linux with Examples

    What is a Orphan Process in Unix/Linux

    View 2 Comments

    2 Comments

    1. rahul2 on April 3, 2021 2:09 am

      not working at all

      Reply
      • Rahul on April 3, 2021 1:04 pm

        What issue are you facing?

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • 20 Basic Linux Commands for the Beginners (Recommended)
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.