Solved

Apache Deny Access to Folder/Contents

Posted on 2009-04-01
5
2,363 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
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…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now