Using htpasswd to allow directory listing of files for a particular folder in Apache

Assuming you are using Apache2 and the regular good old LAMP stack.

Use Case Scenario : You have the website located at /var/www/html and have a folder in it called PDFs where there are generated PDFs stored in there. And you want to periodically see which PDFs are located in that folder by going to https://my-domain.test/PDFs and have all the PDFs listed on which you can click on a PDF and the browser will either download it or have it viewable in the browser itself.

cd /var/www/html/PDFs
htpasswd -c .htpasswd sammy
New password: 
Re-type new password: 
Adding password for user sammy

Now create the .htacess file in PDFs to let it know that it is password protected for directory listing.

touch .htaccess
vi .htaccess
Options +Indexes

<Files *>
AuthType Basic
AuthName "Authentication Required"
AuthUserFile /var/www/html/PDFs/.htpasswd
Require valid-user

Now, when you goto https://my-domain.test/PDFs in the browser, you'll be prompted for a username and password - enter sammy as the username and enter the password that you set before.

0
Subscribe to my newsletter

Read articles from Anjanesh Lekshminarayanan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjanesh Lekshminarayanan
Anjanesh Lekshminarayanan

I am a web developer from Navi Mumbai working as a consultant for cloudxchange.io. Mainly dealt with LAMP stack, now into Django and trying to learn Laravel and Google Cloud. TensorFlow in the near future. Founder of nerul.in