There are tons of Web sites out there that talk about how to password protect a directory using Apache's .htaccess and .htpasswd files. However, I had to visit quite a few of them to finally get this to work for me for this simple use of .htaccess and .htpasswd files (these files can be used for many other purposes as well). So I have written this guide to help do this simply and easily:

Step 1

Create a file in the directory you wish to protect called .htaccess with the following contents:

AuthType Basic
AuthName "Restricted Files"
AuthUserFile /usr/local/somedir/.htpasswd
Require valid-user

Don't forget to change the AuthUserFile line full path to the correct location of this directory you wish to protect.

Step 2

Many flavors of unix come with a command line utility for generating .htpassword files. Use it with the following command in the directory you wish to place the .htpassword file:

htpasswd -c .htpasswd username

It will then prompt you for the password and create the file. (the -c option creates a new file, just type htpasswd for usage if adding additional users or passwords)

Step 3

Issue the following commands in that same directory:

chmod 644 .htpasswd

chmod 644 .htaccess