Prevent image hotlinking .htaccess

To prevent image hotlinking using `.htaccess`, you can add the following code to your `.htaccess` file in the root directory of your website:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

In the above code, make sure to replace `` and `` with your actual domain names. This code uses mod_rewrite to perform the following steps:

1. Activate the rewrite engine.
2. Check if the `Referer` header is not empty.
3. Check if the `Referer` header does not match your domain(s) using regular expressions. You can add more `RewriteCond` lines for additional domains.
4. Check if the requested file has a `.jpg`, `.jpeg`, `.png`, or `.gif` extension (you can add more extensions if needed).
5. If all conditions are met, return a 403 Forbidden response (`[F]`) to block access to the image.
6. The `[NC]` flag makes the match case-insensitive.
7. The `[L]` flag stops the rewriting process.

This code will prevent direct access to images from external websites, but it allows them to be displayed on your own website.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Changing PHP settings in an .htaccess file

To change PHP settings using an `.htaccess` file, you can use the `php_value` directive. This...

Control file extensions with an .htaccess file

To control file extensions using an `.htaccess` file, you can use Apache's mod_rewrite module to...

How can I redirect and rewrite my URLs?

To redirect and rewrite URLs, you can use a combination of server configuration and URL rewriting...

Deny access to a site with an .htaccess file

To deny access to a site using an .htaccess file, you can use the `Deny` directive in combination...

Force your site to load securely code for .htaccess

To force your website to load securely using the .htaccess file, you can add the following code...