how to prevent direct access to images

how to prevent direct access to images through server

how to prevent direct access to images. To protect your images or media file you should use apache rewrite rules. To protect being linked on another website. To protect your images or media file you should use apache rewrite rules. To protect your images or other files from being linked or used on another website. Due the this issue your images may be used by many sites.

how to prevent direct access to images

how to prevent direct access to images
how to prevent direct access to images

The Apache Server’s Mod Rewrite Engine can examine the name of the document requesting a file of a particular type. If the URL of the page requesting the image file is from an allowed domain, display the image. Otherwise, return a broken image.

There are many people who try to use your website images. That will cut your bandwidth so using this code is good idea.

You can use following apache code in apche config file or put following code in your .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://images.purabtech.in/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.purabtech.in/files/.*$ [NC]
RewriteRule .*\.jpg$
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://images.purabtech.in/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.purabtech.in/files/.*$ [NC]
RewriteRule .*\.jpeg$
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://images.purabtech.in/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.purabtech.in/files/.*$ [NC]
RewriteRule .*\.png$
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://images.purabtech.in/.*$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.purabtech.in/files/.*$ [NC]
RewriteRule .*\.gif$
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]

Second thing you should do. you should create the dummy index.html file and put in your images folder for so any visitor will not see the directory listing of images.

If you use above code then when other domain try to see or use the your images then they will see the following error message.

it will result in a Forbidden error.
Still if you are having any issue or question then write to me.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.