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> 

3 Answers 3

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> 
2

Instead of file directive try using location :

<Location "path/to/folder-or-file"> Order Allow,Deny Deny from all </Location> 
1
  • 3
    The Location directive isn't valid in .htaccess files, as the author requested. This will cause a 500 error. Commented Jan 16, 2020 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.

10
  • 1
    If i use DirectoryMatch like you have described, i have Internal Server Error, the folder are in the same directory oh .htacces (/) Commented Aug 6, 2015 at 15:12
  • Are you doing this via the .htaccess file? In which case you will require AllowOverride Limit set on that directory by your main apache configuration. Commented Aug 6, 2015 at 15:17
  • Yes, i need put into .htaccess file, i have AllowOverride All in apache configuration Commented Aug 6, 2015 at 15:20
  • What is your apache error log showing? Commented Aug 6, 2015 at 15:28
  • 3
    To the title i have specified "whit .htaccess" Commented Aug 6, 2015 at 15:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.