Password protect Website via htaccess
- Datum:
- 20. Juni 2026
- Tags:
- Unix
Sometimes you don’t want to habe your site visible to public. One way of managing that is to use settings in the .htaccess file.
1. Create the .htpasswd file
a) For the entire website
cd ~/example.com
b) or just a subdirectory
cd ~/example.com/members
Check your full path to the domain or subdirectory:
pwd /home/username/example.com
Create the actual .htpasswd file with access for guest1
htpasswd -c /home/username/example.com/.htpasswd guest1
q) The htpasswd process will ask for a password, enter and confirm it. It will look like that:
guest1:$apr1$bkS4zPQl$SyGLA9oP75L5uM5GHpe9A2
b) confirm or correct the permission to 644
chmod 644 .htpasswd
Create or modify the .htaccess file
The .htaccess and .htpasswd files need to be in the same directory
a) Entire Website
#Protect Directory
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user
b) a single file
#Protect single file
<Files admin.php>
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user
</Files>
c) Multiple files
#Protect multiple files
<FilesMatch "^(admin|staff).php$">
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user
</FilesMatch>