Skip to main content

Command Palette

Search for a command to run...

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

Published
1 min read
Using htpasswd to allow directory listing of files for a particular folder in Apache
A
I am a web developer from Navi Mumbai. Mainly dealt with LAMP stack, now into Django and getting into Laravel and Cloud. Founder of nerul.in and gaali.in

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.