Password protecting directories with htaccess

Task

Password protecting a directory on your website using the htaccess file – this tutorial is only for linux servers and you will also need shell access to your server.

Explanation

First, make a file called ".htaccess" – note that there is no "filename" just an extension. You can save a file with this name in a text editor such as notepad, but using the quotations – they will stop notepad from adding .txt to the file, for example:

".htaccess"

Add the following to the .htaccess file:

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName “My Secret Folder”
Require valid-user

Upload this file to the directory you want to be protected.
Now log on to the shell of your server, and type the following:

htpasswd -c /full/path/to_a_private/location_on_server/.htpasswd username

You will then be prompted to enter a password for that username – which will then automatically be encrypted.
Afterward set the permissions of the password file to 644.
If you want to add additional users, remove the -c attribute:

htpasswd /full/path/to_a_private/location_on_server/.htpasswd usertwo

Thanks

N/a