Solved

Apache Deny Access to Folder/Contents

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Read about why website design really matters in today's demanding market.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

912 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

17 Experts available now in Live!

Get 1:1 Help Now