Skip to content

How do I password-protect my website using .htaccess

May 10, 2010

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 foloowing 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 amit
New 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 commad # 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.

Click here a World Of Information.


From → Apache

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: