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.

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.

vikash:sPoDKP/ULcZQ.
rahul:93b7gyAizeUGs

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 Comment