Hotlinking occurs when other websites link directly to files like images on your server, causing unnecessary server load and bandwidth usage. This can slow down your website and increase hosting costs. Preventing hotlinking helps you manage server resources efficiently.

Apache offers tools to stop hotlinking and protect your media files. By enabling the mod_rewrite module, you can restrict access to these files so that only your domain can serve them. This method involves editing the Apache configuration or the .htaccess file.

Blocking hotlinking is important to ensure your website runs smoothly. It also prevents unauthorized use of your content on other sites, keeping your resources secure and under your control.

Steps to disable hotlinking in Apache:

  1. Enable the rewrite module for Apache.
    $ sudo a2enmod rewrite # For Ubuntu, Debian, and SUSE variants
    Enabling module rewrite.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    • Distributions with a2enmod support can run the command above without manually enabling the modules.
    • CentOS and Red Hat have the module enabled by default, so no manual action is needed.
    Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp
    a2enmod support yes yes no no no no
    Modules to install none
    Module name n/a rewrite
    Loadmodule directive n/a LoadModule rewrite_module <module_locations>/mod_rewrite.so
  2. Locate the directory containing your media files.
  3. Edit the Apache configuration file or create a .htaccess file in the target directory.
    $ sudo vi /etc/apache2/apache2.conf
  4. Add rewrite rules to restrict access to your media files.
    <Directory "/var/www/html/images">
        RewriteEngine on
        RewriteCond %{HTTP_REFERER} !^$
        RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
        RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?anotheralloweddomain.com [NC]
        RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
    </Directory>

    If using .htaccess, the Directory directive is not required.

    Directive/Rule Description
    RewriteEngine on Enables the rewrite engine.
    RewriteCond %{HTTP_REFERER} !^$ Checks if the referrer is not empty.
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC] Checks if the referrer is not from your domain, with or without www, and either http or https.
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?anotheralloweddomain.com [NC] Checks if the referrer is not from another allowed domain, with or without www, and either http or https.
    RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L] Blocks access to JPG, JPEG, PNG, and GIF files if the above conditions are met. The flags used are: NC (No Case - case insensitive), F (Forbidden - sends a 403 Forbidden status code to the client), and L (Last - stops processing further rules if this one is matched).
  5. Save the changes and close the editor.
  6. Restart the Apache service to apply the changes.
    $ sudo systemctl restart apache2
  7. Test the configuration by accessing the files from a different domain.

    Ensure you clear your browser cache or use a different browser to accurately test the configuration.

Discuss the article:

Comment anonymously. Login not required.