Howto: Password Protect a directory using .htaccess

How to Password Protect a Directory using .htaccess?

You may need to password protect a directory in order to limit the sharing of files under it OR may need to protect a private area. You can password protect a directory using a .htaccess file which has to be placed under a directory which needs to be protected.

Create a .htaccess file

vi /home/username/.htaccess

Once created, add the following lines to it:

AuthUserFile /home/username/.htpasswd
AuthName “Private Area”
AuthType Basic
require valid-user

where, ‘username’ is the actual username of your domain. Now, create a .htpasswd file under the /home/username/ directory.

vi /home/username/.htpasswd

In order to grant access to the directory for specific users, you need to place all the users along with their passwords in the below format:

username1:encryptedpassword
username2:encryptedpassword

There is no limit in adding users to this file, just make sure each user should be on a separate line and you can encrypt passwords using any available tool on the internet.

This entry was posted on Friday, September 18th, 2009 and is filed under Linux Administration. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.