How to stop direct URL access to images with htaccess

Hi, Do you know how to prevent the direct URL access to the web?

In my current project, somebody has copied my whole images from the site with direct access. In this case, we lose the data and this data went to untrusted persons they definitely misuse our content.

To prevent this type of attack from the hackers to disable the direct URL access. This is a simple way to do by using the .htaccess file.
.htaccess
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.ltd [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.ltd.*$ [NC] 
RewriteRule \.(gif|jpg|js|txt)$ /messageforcurious [L]

When anybody tries to access the direct URL it returns 403 but allows them to be displayed on the site.

This is a Rosi pov's rule it works great!. So taking rosipov's rule to redirect any direct request to {gif,jpg,js,txt} files to 'messageforcurious'

NOTE: it is possible that when you open some page with image and then copy that image's path into the address bar you can see that image, it is only because of the browser's cache, in fact that image has not been loaded from the server

In your localhost try this code
.htaccess
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] 
RewriteRule \.(gif|jpg)$ - [F]
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff


EmoticonEmoticon