Htaccess can be used to password-protect directories on your web sites.

You can do this Password Protection very easily by using below steps
Step 1. Change to the directory that you wish to protect

In the following example we wish to protect a directory called pvt in our htdocs directory.
[root@amit ]# cd /usr/local/apache2/htdocs/
[root@amit.htdocs] # mkdir pvt

Step 2. Create a file named .htaccess
Use your favourite editor to create a file called .htaccess . In the below example we will use vim.
[root@amit.htdocs] # vim .htaccess
Add following lines in .htaccess file

AuthUserFile /usr/local/apache2/conf/.htpasswd #### path of .htpasswd file
AuthName "My Private Password Protect Directory" ### title for the username/password input box.
AuthType Basic
Require valid-user amit #### vaild user name

Step 3. Create the .htpasswd file by adding users
[root@amit.htdocs] # htpasswd -c /usr/local/apache2/conf/.htpasswd amitNew password:
Re-type new password:
Adding password for user amit

This creates a file .htpasswd containing the username amit. Then it will be prompted for a password for amit, which will be stored in the .htpasswd file
So, to create a new users and change the password for existing users, switch to the directory that contain .htpasswd, run this command

 # htpasswd -c .htpasswd username

Step 4. Set the permissions on your .htaccess and .htpasswd file
Now use chmod command to chage permission of .htaccess & .htpasswd to make it world-readable

[root@amit.htdocs] # chmod 644 /usr/local/apache2/htdocs/pvt/.htaccess
[root@amit.htdocs] # chmod 644 /usr/local/apache2/conf/.htpasswd

Now you done everything !!!!
Now, anytime you attempt to view your password protected directory (pvt ), any file within it, or recursively any subdirectory of it, you will be prompted for a username and password,

From the desk of Amit Maheshwari
 
Top
Maintained By Amit Maheshwari