0

I'm working on a project, a little one. And I'll be interesting in disallow the direct access to files by URL.

Like you have a folder called "stock" where is situated all your images. You take the image cat, for example, and I want to disallow direct access to this file by this URL mysite.com/stock/cat.jpeg

But, in the same time, I want to be able to use these images on my website.

What is the best way to realise that ? I used .htaccess in the "stock" folder, with a Deny from all. But I wasn't able to use the file.

For the moment, I work on the localhost, but I'll put this live. And my framework (PHP) is CodeIgniter.

Thanks and have a good day :)

Äasgard
  • 15
  • 3
  • Possible duplicate of [(htaccess) How to prevent a file from DIRECT URL ACCESS?](http://stackoverflow.com/questions/10236717/htaccess-how-to-prevent-a-file-from-direct-url-access) – Takarii Sep 15 '16 at 12:52
  • Try this answer http://stackoverflow.com/questions/21647750/how-to-view-images-from-protected-folder-with-php – Vipin Kr. Singh Sep 15 '16 at 12:55

1 Answers1

0

You can still use the .htaccess, I've provided some code that you can use, remove www. domain & tld and replace it with what fits you best.

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]

This should make it so that only local access to allowed.

If you are asking about allowing an IP Address to be allowed to connect, then you could use something like:

order deny,allow
deny from all
allow from <your ip> 

If the above does not work, you could also edit your .htaccess file and see if this would work:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.123$
RewriteRule .* - [F]

Some of the code here was from this answer.

Community
  • 1
  • 1
FluxCoder
  • 1,266
  • 11
  • 22