Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»How to Prevent Image Hotlinking in Apache with .htaccess

    How to Prevent Image Hotlinking in Apache with .htaccess

    RahulBy RahulSeptember 3, 20192 Mins ReadUpdated:June 30, 2020

    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 that images on their website using your domain link. In this case whenever the other user’s website loads, The image is load from your website. This can increase the hosting cost for you.

    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 to 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 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 hot linking 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 hot linking 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 image to be hot link by the unknown domains in Apache server.

    Apache hotlinking htaccess Images
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install Django on Fedora 35/34/33
    Next Article How to Install Composer on MacOS

    Related Posts

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    (Resolved) – ReactJS 404 Error on Page Reload

    2 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    Adding a New SSH Key in GitHub

    Updated:April 1, 20223 Mins Read

    13 Best Linux Terminal Emulators and Bash Editors

    8 Mins Read

    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

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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