How to password protect a site with a .htaccess file?
1. Create the .htpasswd file
a) For the entire website
cd ~/example.com
or just a subdirectory
cd ~/example.com/members
b) Check your full path to the domain or subdirectory:
pwd /home/username/example.com
c) Create the actual .htpasswd file with access for guest1
htpasswd -c /home/username/example.com/.htpasswd guest1
d) The htpasswd process will ask for a password, enter and confirm it. It will look like that:
guest1:$apr1$bkS4zPQl$SyGLA9oP75L5uM5GHpe9A2
e) confirm or correct the permission to 644
chmod 644 .htpasswd
2. Create or modify the .htaccess file
The .htaccess and .htpasswd files need to be in the same directory
Entire Website
#Protect Directory
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user
a single file
#Protect single file
<Files admin.php>
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user
</Files>
Multiple files
#Protect multiple files
<FilesMatch "^(admin|staff).php$">
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user
</FilesMatch>