Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I currently have a folder located at /var/www/pictures, with /var/www being the document root for my web server. I want to have the pictures folder to throw a 403 forbidden error if a user tries to access the folder by simply going to, but I also need a php script located in /var/www to be able to display an image from /var/www/pictures when a user goes to Is there anyway to do this?

Thanks in advance for any and all help!

share|improve this question
up vote 3 down vote accepted

You can use .htaccess . Simply create such a file in your picture directory and enter the following directives:

Order allow, deny
Deny from all

See the manual for details about the Order and Deny directives.

Note that the error would be 403 Forbidden instead of 404 Not Found.

Note that this will deny all access via HTTP to the files in your directory. Your other PHP files can include or require those files, as this is a purely server-side operation. If you for some reason need one PHP script to execute another via HTTP, then you could change the order to deny, allow and also add a line Allow from in order to allow local requests to proceed or any specific ip.

share|improve this answer
Does it matter what I name the .htaccess file? – Allfer2 Jul 23 '14 at 3:52
by default the name must be .htaccess see the httpd.conf for the module for htaccess code. For reference: – TBI Infotech Jul 23 '14 at 3:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.