How to password protect directory through .htaccess and .htpasswd linux

Summary: In this post i explained how to protect your directory access through .htaccess. When we are going to use some applications in private environment like server monitoring through web access. Some time the demo project needs to be password protected from public access. The most prominent method is to use the .htaccess rules which will prompt you for user-name and password like we have while accessing any control panel.

How to password protect directory through .htaccess the procedure is listed below.

Step-1: First of all login to your server through ssh terminal. Find the directory which you want to password protect. For example i want to password protect following directory /home/user/domain/secretfolder/ . Create a .htaccess file with within the directory secretfolder .

vim .htaccess

AuthUserFile /home/user/domain/secretfolder/.htpasswd # please the location of .htpasswd file should be known to you. if not don’t wast your time.
AuthType Basic
AuthName “Login Details”
Require valid-user

Step-2: Now  Create .htpasswd file with your desired username and password using the following commands.

htpasswd -c /home/user/domain/.htpasswd imran

passwd: typeyour password

Step-3: Now open your browser and access the protected directory in your brower.

http://mydomain.com/secretfolder

Selection_029
Now whenever anyone accesses that directory or any files in it, they will be asked for login details. You web directory is now secure.

If you want to give access to more then one user you have to add entries to the file .htpasswd. For that use the command listed below

htpasswd /home/user/domain/.htpasswd user1

passwd user1:

Now new user access the directory through the new credentials.

That’s it

Leave a Reply

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