Have you ever wanted a specific directory in your website to be accessible only by a selected number of people.
Protecting files on your website from unauthorized users is very important and actually fairly easy. Although, there are numerous methods to password protect directory on a website but we will accomplish this with the help of .htaccess because it is as secure method as you can or need to get in everyday life.
In order to password protect directory, the first thing you will need to do is create a file called .htpasswd. This is the file that the server will check before allowing access to anything in the same directory. To non tech savvy users, there are many online tools available for generating a .htpasswd file. And below you will find a few online .htpasswd generating tools to get you started. You will need to insert username and corresponding password (of your choice to get access to protected directory) on these .htpasswd generators to create a .htpasswd file. After creating .htpasswd file, upload it to your server.
You can add this snippet to your .htaccess file to password protect any specific directory with access granted by generated .htpasswd file. Make sure you change path to .htpasswd file in below snippet before you instet it in you .htaccess file.
# password protect directory on your website
AuthName "Secure Area"
AuthType Basic
AuthUserFile /path/to/htpasswd/file/.htpasswd
AuthGroupFile /dev/null
require valid-user