Protect Directory With Password Using htaccess

In this tutorial we will learn how to protect directory of website using htaccess to restrict unauthorized access. When we talk about website then must think about its security. It is very important to keep your folder and file secure. In this post we will discuss how to protect your folder or file using htaccess file. This method is easy and reliable to protect from unauthorized access of folder or file of your website.

Protect Directory With Password Using htaccess
Protect Directory With Password Using htaccess

To protect your folder using htaccess file you required two files which are going to discuss in detail. Both file we have to use in root directory of project to protect its particular folder.
• .htaccess file.
• .htpasswd file.
Now we will see which code we have to use in both file to protect folder.
.htaccess file

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/directory/.htpasswd
require valid-user

The above given snippet protect the folder or directory and for this we have to write directory path.
.htpasswd file.


The given above snippet keeps the username and password of authorized user per line to access directory or file. The password used in this file is generated from hash method and that is why it is secured.To use the password you have to generate using hash method. That’s all.

Leave a Reply

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