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.


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

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

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

(not tested)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.