Crmgo Blog

Using .htaccess and .htpasswd files

Using .htaccess and .htpasswd files

to set a password for access to specific pages or sections of your website. First, create a .htaccess file in the desired subdirectory, and add the following content: AuthType Basic AuthName "Access is Denied. Enter password" AuthUserFile /path_to_file/.passwords <Limit GET POST> Require valid-user

Using .htaccess and .htpasswd files

to set a password for access to specific pages or sections of your website.
First, create a .htaccess file in the desired subdirectory and add the following:
AuthType Basic
AuthName "Access is Denied. Enter password"
AuthUserFile /path_to_file/.passwords
<Limit GET POST>
Require valid-user
</Limit>
Next, create a password file, the path to which is specified as a parameter to the AuthUserFile directive.
Passwords in this file must be encrypted, which can be achieved using the htpasswd program included with Apache.

For detailed information on usage, you can read the Manual Page: htpasswd or consult the man pages. You can also simply run htpasswd in a Unix shell:
$ htpasswd
Usage:
htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username passwordhtpasswd -n[mdps] username
htpasswd -nb[mdps] username password
-c Create a new file.
-n Don't update file; display results on stdout.
-m Force MD5 encryption of the password.
-d Force CRYPT encryption of the password (default).
-p Do not encrypt the password (plaintext).
-s Force SHA encryption of the password.
-b Use the password from the command line rather than prompting for it.
.....

So, we don't have a password file yet, and we need to create one:
$ htpasswd -c .passwords test
New password:
Re-type new password:
Adding password for user test

After this, a .passwords file will be created containing the user test and their encrypted password:
$ cat .passwords
test:WE.MwmyNsIG76

Now, we want to add another user. Since the password file already exists, we simply omit the ‘-c’ key:
$ htpasswd .passwords test1
New password:
Re-type new password:
Adding password for user test1
$ cat .passwords
test:WE.MwmyNsIG76
test1:Eeg7zFPfq32BM

We specified valid-user as a parameter to the require directive. This means that any user present in the password file can access the protected resource. However, it is more convenient to keep all passwords in one file and grant access to specific resources only to certain users.
To do this, let's demonstrate an example where access is granted only to the user test:

AuthType Basic
AuthName "Access is Denied. Enter password"
AuthUserFile /path_to_file/.passwords
<Limit GET POST>
Require user test
</Limit>

You can also group users and grant access to groups rather than individual users.
This can be done using the AuthGroupFile directive:

AuthGroupFile /path_to_file/groups

In the groups file, create groups like this:
group1: user1 user3
group2: user2 user4
group3: user1 user3
Accordingly, use the require directive as follows:

AuthType Basic
AuthName "Access is Denied. Enter password"
AuthGroupFile /path_to_file/groups
AuthUserFile /path_to_file/.passwords
<Limit GET POST>
Require user test
Require group group1
</Limit>

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.