Directory listing in htaccess. Allow, Deny, Disable, Enable Directory Listing in .htaccess

When a web browser is pointed to a directory on your web site which does not have an index.html file (or any other index file) in it, the files in that directory can be listed on a web page. Let us see few snippets that can be added in htaccess file to allow or avoid directory listing in apache server.

Enable / Disable directory Listing

To allow a web server to produce a directory listing, whenever you point a directory without index file. Add following line in your .htaccess file.
Options +Indexes # or # IndexIgnore *
Code language: HTML, XML (xml)
To disable or prevent the directory access add following line in your .htaccess file. If user points the browsers to a directory which does not have index file then in this case 403 error will be
Options -Indexes
Code language: HTML, XML (xml)
Following is the error page that gets displayed when we try to access any directory without index file.

Change Listing style

You may want to display other details while showing the directory listing. This includes file icons, file size, modification date and more. This can be done by adding fancy style to your htaccess file. Add following snippet in .htaccess file.
IndexOptions +FancyIndexing
Code language: HTML, XML (xml)
To remove the fancy directory listing or to display normal directory listing, use -FancyIndex.
IndexOptions -FancyIndexing
Code language: HTML, XML (xml)

Ignore files with specific extension

It may happen that you may need to ignore certain files to get displayed in directory listing. This can be achieved using IndexIgnore directive in .htaccess file. Following snippet will not display .zip and .txt file in directory listing.
IndexIgnore *.zip *.txt
Code language: HTML, XML (xml)

Modify Index File

It is possible to change the default index file from index.html (index.php, index.jsp …) to any other file. Following line will change the index file to Home.html.
DirectoryIndex Home.html
Code language: HTML, XML (xml)

View Comments

  • Is there a way to disable all browsing all directories except one?

    For example, after applying "Options -Indexes", can I create a rule to list items inside the '/pdf' folder?

    Thanks.

    • Hi Ricardo,
      There is a way to achieve this. If you want to disable directory listing in a folder and all its subfolders except one subfolder say /pdf. You have to create .htaccess file in root folder and the code to disable directory access: Options -Indexes. And then create another .htaccess file in the subfolder /pdf and add code to allow directory listing in it: Options +Indexes.

  • Thanks Viral, I was looking for a solution for what Ricardo asked, and yours worked perfectly. Thanks! :)

  • The IndexIgnore * line seems to cause the whole thing not to work. This line tells the server to ignore all files in the directory listing. When I took out that line, I was able to see the directory properly.

    Thanks!

  • That's very good information.
    But, is there a way to set these listing permissions per directory, instead of document root?

    Thanks..
    Vas

Share
Published by
Viral Patel
Tags: directory listing How-To htaccess

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago