Password Protect a Directory Using .htaccess

Hello there. Today we’re gonna look on how to password protect a specific folder with help of apache and it’s fantastic .htaccess file.

Lock Folder

This is done in a moment. Start by entering the folder you want to protect. Open the .htaccess file with your favorite editor and type in the following:

AuthType Basic
AuthName "restricted area"
AuthUserFile /var/www/vhosts/my-domian.com/httpdocs/folder/.htpasswd
require valid-user

AuthName is a text that will appear when trying to enter the folder and you can type just anything.
AuthUserFile The path to your .htpasswd file. It’s recommended to put this file in the same folder you want to protect. The .htaccess and .htpasswd files should be protected by the apache server by default so no user can download those two files.

If you don’t know your complete AuthUserFile path you could create a php file with “phpinfo();” in it. This will show you the correct path to your html folder.

Ok. Now we need to create the .htpasswd file. The first time you need to add the “-c” option

htpasswd -c .htpasswd username

You will now be asked to type in a password. Your are done. Now try to enter the folder. 
To add more user you do the same command without the -c option (it means create and will give you an error if the file already exist)

htpasswd .htpasswd username2

 

Cheers
/jima

Leave a Reply

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