Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deny from all in subdirectory htaccess not overriding file rules in root htaccess

I've got a situation where I'm trying to deny access to all files in a subdirectory of my website. I have added an htaccess file to this subdirectory and added a deny from all directive to it. However, I also have an htaccess file in the site root, which allows specific file types, and it seems like these file types are still accessible in the subdirectory even though I no longer want them to be. I have a workaround for this (see below), but I feel like there must be a better way. Here are my two htaccess files:

Root .htaccess

# Deny access to everything by default
Order Deny,Allow
deny from all

# Allow access to html files
<Files *.html>
    allow from all
</Files>

Subdirectory .htaccess

# Deny access to everything
deny from all

Workaround:

Subdirectory .htaccess

# Deny access to everything
Order Allow,Deny
deny from all
<Files *.*>
    deny from all
</Files>

This does what I want, but I feel like there should be a way to make the deny from all statement work by itself. Does anyone know how?

like image 592
FrolickingFerret Avatar asked Jul 12 '13 09:07

FrolickingFerret


2 Answers

You can have your Root .htaccess like this

# Deny access to everything by default
Order Deny,Allow
deny from all

# Allow access to html files
<Files *.html>
    allow from all
</Files>

# Deny access to sub directory
<Files subdirectory/*>
    deny from all
</Files>

There is no need for a separate .htaccess in the sub directory.

You are allowing access to all html files in your .htaccess in the Root Directory and not denying it anywhere in the sub directory in the first case. Apache parses all your rules and uses the last matching rule, unlike firewalls (which uses the first rule match hit). The global rules are read first and the the specific rules later.

like image 129
bansi Avatar answered Oct 29 '22 03:10

bansi


Order of directives in apache is really not obvious.

You have a complete description of it on the documentation "How the sections are merged".

Here is an excerpt:

The order of merging is:

  • <Directory> (except regular expressions) and .htaccess done simultaneously (with .htaccess, if allowed, overriding <Directory>)
  • <DirectoryMatch> (and <Directory ~>)
  • <Files> and <FilesMatch> done simultaneously
  • <Location> and <LocationMatch> done simultaneously
  • <If>

So what happens is that your <File> directive is handled after the Directory ones (as a .htaccess is in fact a Directory directive for the current directory).

It works in your given exemple as theses files directives are in fact nested in the .htaccess Directory directives, and the second File directive is applied after the parent directory one.

You cannot use a <FileMatch> directive in the parent, where files from the subdirectory would be excluded, as fileMatch is only working on the file name, and not on the file path. But you could maybe try with a LocationMatch, but it would maybe end in a quite complex one, to forbid also location hacks with dots.

In fact the solution I would use here is a RedirectMatch in the parent folder:

RedirectMatch 403 ^.*\.html$
like image 30
regilero Avatar answered Oct 29 '22 04:10

regilero