Score:2

Apache Options -Indexes for sub directories

ca flag

Per How can I prevent people from looking at a listing of files in parent directory if I haven't uploaded index.html? I can see how to disable listing of files if there is no index.* file.

How can i allow file listing for the root directory, but not any other sub directory?

Thanks in advance.

Score:3
br flag

Normally sub directories will inherit the same settings you apply to their parent directory in Apache. And, as far as I know, you can't change that and there doesn't exist a way to limit the scope of directives to only a single directory level.

That means that you need to:

  • set the option/directive you want on the parent directory
  • change/override/negate that option/directive for all (current and new) subdirectories.

Rather than doing that for every subdirectory individually it is easiest to that do that with the DirectoryMatch directive.

In you main httpd.conf (or an include) set

<Directory "/path/to/example.com/">
  # enable directory listings
  Options +Indexes
</Directory>


<DirectoryMatch "/path/to/example.com/.*/">
  # Disable directory listings for all sub directories of /path/to/example.com/
  Options -Indexes
</Directory>

(not tested)

Score:2
kz flag

Since you tagged the question .htaccess, you could do something like the following in the root .htaccess file on Apache 2.4:

# Default - Directory listings disabled (mod_autoindex)
Options -Indexes

# Override - Allow directory listings for the root only
<If "%{REQUEST_URI} == '/'">
    Options +Indexes
</If>
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.