====== Password protecting your site with an .htaccess file ====== Source: [[https://help.dreamhost.com/hc/en-us/articles/216363187-Password-protecting-your-site-with-an-htaccess-file|External Link]] ===== Step 1 — Create the .htpasswd file ===== In Linux shell navigate into the directory you wish to password protect. Run pwd to confirm the full file path to this directory. You'll need this full path in the next step. [server]$ pwd /home/username/example.com Create an .htpasswd file by running the following command in the directory you wish to password protect. This uses the htpasswd utility and the full path to the directory. For example, if the username you're creating to log in is user1, run the following: [server]$ htpasswd -c /home/username/example.com/.htpasswd user1 Enter a password for the new user named user1. The cli in your .htpasswd file then displays the encrypted password like this: nano ./htpasswd user1:$apr1$bkS4zPQl$SyGLA9oP75L5uM5GHpe9A2 Run it again (without the -c option) for any other users you wish to allow access to your directory. Confirm the permissions are set to 644 by running the following command. [server]$ chmod 644 .htpasswd ===== Step 2 — Create the .htaccess file ===== Make sure to add this .htaccess file in the same directory you added the .htpasswd file. [server]$ nano .htaccess ====== cli examples to add to the .htaccess file Protect an entire website This example password protects an entire website by placing the files in the site's main web directory. ====== Make sure to change the highlighted lines to your username, domain name, and specific file names. #Protect Directory AuthName "Dialog prompt" AuthType Basic AuthUserFile /home/username/example.com/.htpasswd Require valid-user Protect a single file This example password protects a single file named admin.php: #Protect single file AuthName "Dialog prompt" AuthType Basic AuthUserFile /home/username/example.com/.htpasswd Require valid-user Protect multiple files This example protects multiple files such as admin.php and staff.php. #Protect multiple files AuthName "Dialog prompt" AuthType Basic AuthUserFile /home/username/example.com/.htpasswd Require valid-user Rest can read on https://help.dreamhost.com/hc/en-us/articles/216363187-Password-protecting-your-site-with-an-htaccess-file