Htaccess password protect directory

You can use htaccess password protection for URL or directory when you do not want provide its access to public.

Let us suppose you want to password protect ‘private’ folder on your website. Here are steps to restrict it.

Htaccess code to protect directory:

In .htaccess file, you need to mention absolute path of your directory password file with AuthUserFile along with other authentication detail (given below). And then, you need to copy this .htaccess file in the ‘private’ directory. Now this directory is not accessible without password mentioned in .htpasswd file.

#Protect Directory
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/mywebsite/public_html/private/.htpasswd
require valid-user

Absolute path is visible when we edit/open any folder in cpanel as well.

Creating the .htpasswd file to store password:

In .htpasswd file, write username and column in encrypted form separated by column. You can generate password using various tools online such as hostingcanada or web2generators.


You need to provide these username and password when accessing protected directory on server.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.