K Ahir
K Ahir

Reputation: 395

htaccess - prevent directory access directly but allow directory access from website

I want to make so no one can access particular directory or files directly from browser. But I want to make so above prevented directory and files are accessible through website.

Using below code, I can prevent direct access of 'data' folder from browser. But when I checked from website, all content that is coming from 'data' folder are not displaying (means not accessible).

RewriteRule ^(data/) - [F,L,NC]

Using below code, I can prevent direct access of below mentioned extensions files from browser. But when I checked from website, all content with these extensions are not displaying (means not accessible).

RewriteRule \.(gif|jpg|wmv)$ - [F]

What changes should I make in .htaccess to make it possible?

Upvotes: 0

Views: 144

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Try this :

RewriteCond %{HTTP_REFERER} !^https://(www\.)?yoursite\.com/ [NC] 
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F]

The problem right now , when someone exploring the page that has one of these files and open the source code , he will be able to browse that file directly from cache unless reloading the page , so if you want to prevent that as well , you could disable cache for specific mentioned extensions but you must know what that means prior to disable caching them .

Upvotes: 0

Related Questions