Prevent an image file from DIRECT URL ACCESS?

Viewed 4914

I want to prevent people from getting the images of my website by typing in the URLs in browser address bar, while allowing them to view the images on when visiting the webpages.

I tried the following .htaccess code:

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

However, it not only restricts direct URL access, but also hides all of images even when visiting the webpage. Now my website looks like a page of text with a lot of image holes.

Can anyone tell me why the above .htaccess doesn't work?

I found it here (htaccess) How to prevent a file from DIRECT URL ACCESS?, but I don't have enough reputation to ask in that post, because I just created this account to post this question.

Thanks.

2 Answers

you can simply type Option -Indexes in the .htaccess file and this will prevent direct URL access to the image file

Related