Hi all. In this post I am going to tell you how to create a protected directory in your website using .htaccess file. Many a time it is a requirement of the website that only authenticated people have an access to files in particular folder. In this article we are going to see the same thing. This post is only for Linux servers only as .htaccess file does not work on windows servers.

Note: Make sure that you should have SSH access of the server.

Go through the following steps to create a protected directory for your site.

  1. Create a .htaccess file into your root directory.
  2. Add the following code to .htaccess file.

AuthName “Password Protect Directory”

AuthType Basic

AuthUserFile /home/username/proteted_dir_name

<Limit GET POST PUT>

Require valid-user

</Limit>

Make sure that the path for “AuthUserFile” in above code is correct in your case.

3. Login to your SSH and fire following command.

“/usr/local/apache/bin/htpasswd -c /home/username/.htpasswd YOUR_USER_NAME”

Conventions in above command:

-  Do not change the path ““/usr/local/apache/bin/htpasswd”

-  The path “/home/username/.htpasswd” should be same where you have created your .htaccess file.

-  “YOUR_USER_NAME” is the user name for your protected directory.

4. After above command it will ask for the password.

5. The command will generate a .htpasswd file which contains user name and password in encrypted form to access     protected directory.

6. And Done. Your protected directory is ready. Now when ever you try to access your protected directory it will ask for user name and password.

IF ABOVE CODE DOES NOT WORK TRY CODE BELOW.


1. Enter the following code into .htaccess file.

AuthUserFile /home/www/davidher/.htpasswd

AuthGroupFile /dev/null

AuthName “Restrected Access”

AuthType Basic

<Limit GET POST>

require valid-user

</Limit>

2. Paste above code in .htaccess file. Here again take care about “AuthUserFile” path.

3. Fire following command on SSH.

4. htpasswd -cmb .htpasswd USER_NAME PASSWORD