How to Password Protect a Folder/Directory Through .htaccess File

In my previous articles, i already discussed what is .htaccess and their applications in web development. In this short tip, i am going to tell you how to secure you folders through .htaccess File.

Password Protect a Folder Through .htaccess

Create one .htaccess file in a folder, which you want to password protected.

Write these codes on .htaccess File

AuthUserFile : Defines the the location of .htpasswd from root. This file contains the username and password.

AuthName : It defines the title of popup authentication. So in my case if popup appears, it shows the title Only Authorized Admin.

require valid-user : It means any valid user is allowed.

If i want only specific user then i write their name

.htpasswd File Creation

Next step is to create .htpasswd file. .htpasswd file contains username and password in the following format.

You can use htpasswd generator to create .htpasswd file. Enter username and password. It generate the combination. Paste the generated combination in .htpasswd file.

Now everything is done. Try to open the files in that folder, it will open the popupbox for authentication. If you enter the credential correct, it will allow. Otherwise it will give 401 Unauthorized.

Password Protect Folder

Unauthorized

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn’t understand how to supply the credentials required.

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.