User Tools

Site Tools


password_protect

Password protecting your site with an .htaccess file

Source: 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:

password_protect.txt · Last modified: 2022/02/05 14:23 by norman