Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 mydomain.com/pictures, 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 mydomain.com/myscript.php. Is there anyway to do this?

Thanks in advance for any and all help!

share|improve this question

1 Answer 1

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 127.0.0.1 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 at 3:52
1  
by default the name must be .htaccess see the httpd.conf for the module for htaccess code. For reference: httpd.apache.org/docs/current/howto/htaccess.html –  TBI Infotech Jul 23 at 3:54

Your Answer

 
discard

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.