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.

I have a few private files in a public folder and I want to block access to them. For example lets say I have the following files tree:

  • DictA
    • FileA
  • FileA
  • FileB
  • FileC

I want to block access to FileB and FileA in the current directory and allow access to the FileA in the DictA directory. The first thing that came to mind was to use the FilesMatch directive as follows:

<FilesMatch "^(?:FileA)|(?:FileB)$">
  Deny from all
</FilesMatch>

The problem here is that FileA inside DictA will also be blocked, which is not what I wanted. I could override that by adding another .htaccess file to DictA but I would like to know if there is a solution which wont involve that.

P.S: I can't move the private files to a separate folder.

share|improve this question

2 Answers 2

You have several methods.
One can be to write a regular expression containing a / as stoping character.

I propose another one, allowing to use any options for Apache:
You define the desired options for the current directory, then you define the opposite options for subdirectories.
<Directory /var/www/mydir/>
Options -Indexes
</Directory>
<Directory /var/www/mydir/*>
Options +Indexes
</Directory>

So for your example, just use deny in the current dir, and allow for the sub-dirs:
<Directory /var/www/mydir/>
<FilesMatch "^(?:FileA)|(?:FileB)$">
Deny from all
</FilesMatch>
</Directory>
<Directory /var/www/mydir/*>
<FilesMatch "^(?:FileA)|(?:FileB)$">
Allow from all
</FilesMatch>
</Directory>

note: how can I indent the code ?

share|improve this answer

mod_rewrite can do this.

RewriteRule ^File[AB]$ - [R=404]
share|improve this answer

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.