Apache prevent hot linking or leeching of images using mod_rewrite howto

https:/‮al.www/‬utturi.com
Apache prevent hot linking or leeching of images using mod_rewrite howto

To prevent hot linking or leeching of images on an Apache web server, you can use the mod_rewrite module and the RewriteRule directive in a .htaccess file.

The mod_rewrite module allows you to define rules for rewriting URLs, and the RewriteRule directive specifies a pattern to match against the incoming URL, and a substitution to apply to the URL if the pattern matches.

To prevent hot linking or leeching of images, you can use the RewriteRule directive to match against the incoming URL and check the Referer header to ensure that the request is coming from your own domain. If the Referer header is not set or does not match your domain, you can return a 403 Forbidden error to block the request.

Here is an example of using the RewriteRule directive to prevent hot linking or leeching of images on your domain:

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

This directive will match against URLs that end with .jpg, .jpeg, .png, or .gif (i.e. image files), and check the Referer header to ensure that the request is coming from your domain. If the Referer header is not set or does not match your domain, the request will be blocked and a 403 Forbidden error will be returned.

Keep in mind that the exact syntax and options for the RewriteRule directive may vary depending on your specific Apache installation and configuration. It is always a good idea to refer to the official Apache documentation for detailed instructions and further information.

Created Time:2017-10-16 14:38:36  Author:lautturi