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

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 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 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.