4

I need to deny access to configuration files under some subfolder. Currently I have this rule but it doesn't work:

<Files ~ "((foo|bar))$">
  Order deny,allow
  Deny from all
</Files>

If I go to www.mysite.com/foo/foo2/file.xml, I can view the file. I need to deny all file accesses into fooand bar recursively

UPDATE

I have tried this this configuration but have an Internal Error

<FilesMatch ".foo\.(*)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
stecog
  • 2,221
  • 10
  • 29
  • 39

3 Answers3

2

"<Files> [...] will be applied to any object with a basename (last component of filename) matching the specified filename". It's not what you want.

Directory* don't work in .htaccess

You have to use mod_rewrite. For example:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^(foo|bar) - [F]
</IfModule>
Mounaam
  • 21
  • 2
2

Instead of file directive try using location :

<Location "path/to/folder-or-file">
    Order Allow,Deny
    Deny from all
</Location>
  • 1
    The Location directive isn't valid in .htaccess files, as the author requested. This will cause a 500 error. – EricP Jan 16 '20 at 16:25
1

I would do regex matching inside the apache config. Then

  • use <DirectoryMatch> instead of <File> (you are regular expression matching on a directory)
  • correct your regex's strictness (specifically the $)
  • simplify your regex (too many brackets)

    <DirectoryMatch "/path/to/toplevel/(foo|bar)">
        Order deny,allow
        Deny from all
    </DirectoryMatch>
    

Note that /path/to/toplevel/ is the file system location of your WWW directory where foo and bar reside.


However, If you want to do it via .htaccess only, create a .htaccess file in every directory that you want to deny (foo, bar, foo/bar etc), and put the line

Deny from all

inside.

Drav Sloan
  • 14,145
  • 4
  • 45
  • 43