Solved

Apache Deny Access to Folder/Contents

Posted on 2009-04-01
5
2,370 Views
Last Modified: 2012-05-06
I have a directory on my server which contains PHP and image files.

I would like to be able to deny access to everything but the image files, the user should see a 403 if they try to access anything other then an image.

They should also not be able to see the index of page.

I have attached my sample code below it correctly denies access to documents but doesn't allow me to access the images.

The rule needs to apply to all documents folder, they could be located directly under the root or in subdirectories.

Expected output should be something like:

documents/folder/ Deny
documents/folder/image.jpg Allow
sub/documents/folder/image.jpg Allow

documents/folder/ Deny
documents/folder/bad.bad Deny
sub/directory/folder/bad.bad Deny
<Directory ~ "documents">
Order allow,deny
Deny from all
</Directory>
 
<FilesMatch "documents(.*?)\.(gif|jpe?g|png)$">
Allow from all
</FilesMatch>

Open in new window

0
Comment
Question by:nick_2007
  • 3
5 Comments
 
LVL 11

Assisted Solution

by:kyodai
kyodai earned 125 total points
ID: 24037347
You can not deny access for a folder but then have access to a file in the folder. Instead of setting "deny" for the folder just remove the browse right from the folder.
0
 

Author Comment

by:nick_2007
ID: 24037374
Could you show the conf settings I would need for this?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24037534
You're on apache 2.x?


# httpd.conf
# match documents/ except .jpg or .png etc. at the end
<Directory ~ "documents/.*+(?<!\.jpg|\.png|\.gif)$">
Order allow,deny
Deny from all
</Directory>

Open in new window

0
 

Accepted Solution

by:
nick_2007 earned 0 total points
ID: 24037562
Turns out FileMatch only checks the filename not the directory path as well.

Using LocationMatch instead fixes the issue and mean I can deny access to non images in the directory.
0
 

Author Comment

by:nick_2007
ID: 24037581
I tried your method caterham it doesn't prevent access to non image files.

Yes it's Apache 2.2.11
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question