Home >> Apache >> How do I password-protect my website using .htaccess

How do I password-protect my website using .htaccess

securityHtaccess 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.


  1. Your blog is so wonderful to keep sight of! You are such a amazing person to do this and share it with all of us. I will continue to look through where you post something good on it. And I will keep your wonderful blog in my memory. Be safe and healthy! God Bless!
    by Jordan Shoes

  2. Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!


Leave a Reply

Your email address will not be published. Required fields are marked *