To password protect a directory in Apache, create a user:hash line with the Htpasswd File Generator, save it in a .htpasswd file outside the web root, then add AuthType Basic, AuthName, AuthUserFile and Require valid-user to .htaccess or a Directory block. Reload if needed, then open the URL and confirm the browser asks for a login.
How to protect a directory in Apache (quick answer)
AuthType Basic
AuthName "Restricted area"
AuthUserFile /home/example/secure/.htpasswd
Require valid-user
AuthType Basicturns on HTTP basic authentication (mod_auth_basic).AuthNameis the text some browsers show in the login prompt.AuthUserFilepoints to the file with theuser:hashlines (mod_authn_file). Use an absolute path.Require valid-userlets in anyone whose login and password match a line in that file.
How to create the .htpasswd line with Toolper
The htpasswd command needs apache2-utils or httpd-tools. The generator builds the same line in the browser with nothing to install.
- Open the Htpasswd File Generator.
- Type a login. Empty logins, and logins containing a colon, spaces or control characters, are refused with a message, because a colon would break the
user:hashformat. - Choose a password. Either type your own (you get a Weak, Fair or Strong label) or let the tool generate one from 4 to 128 characters. For generated passwords you can switch lowercase, capitals, digits and symbols on and off (at least one set stays on), and the page shows strength in bits of entropy.
- Keep the algorithm on bcrypt, the default (
$2y$, cost 10). - Press Copy line. You can also copy only the password to put it in a password manager.

- Paste the line into your
.htpasswdfile, one user per line.
Generation uses crypto.getRandomValues in your browser, without bias. Only the length, the character sets and the algorithm are remembered in your browser; the privacy check is in the test results below.
The tool builds one line per run. For many users or an existing file, use the command line (see below).
Add the directives: .htaccess or Directory block
Use .htaccess when you can't edit the main server config (shared hosting), and a Directory block when you can.
Option 1: .htaccess. Create a file named .htaccess inside the folder you want to lock, with the four directives from the quick answer.
Apache only reads this file if the parent configuration allows it with AllowOverride AuthConfig (or AllowOverride All). With AllowOverride None, the file is silently ignored. No restart is needed: Apache reads .htaccess on each request.
Option 2: Directory block. Put the same rules in your virtual host or main config:
<Directory "/var/www/html/private">
AuthType Basic
AuthName "Restricted area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
No .htaccess and no AllowOverride are needed here. Apache reads the main config only at startup, so reload it after editing (for example sudo systemctl reload apache2, or httpd on some systems).
Apache 2.2 vs 2.4: Require valid-user syntax
Apache 2.4 handles access control through mod_authz_core, and Require valid-user is the line to use. Tutorials written for 2.2 use Order, Allow, Deny and Satisfy, which belong to the older module set. Copying them onto a 2.4 server is a common cause of failure. Don't mix the two styles in one block.
Alternative method: htpasswd command line
With shell access and apache2-utils (Debian, Ubuntu) or httpd-tools (RHEL family) installed, htpasswd writes the file for you:
htpasswd -cB /home/example/secure/.htpasswd alice
htpasswd -B /home/example/secure/.htpasswd bob
-c creates the file, so use it only for the first user; running it on an existing file starts a new one. Without -c, the command adds a user or updates that user's password. -B selects bcrypt.
Use the command line for several users or an existing file, and the browser generator when you need one ready line without installing anything.
Which hash did we verify?
| Scenario | Hash prefix | Line length | Hash length | Time to line | Verified |
|---|---|---|---|---|---|
| bcrypt, generated password (20 characters) | $2y$10$ | 66 | 60 | 22 ms | yes |
| bcrypt, generated password (32 characters) | $2y$10$ | 66 | 60 | 89 ms | yes |
| apr1, own password “test” | $apr1$ | 39 | 37 | 206 ms | yes |
| SHA1, own password “test” | {SHA} | 35 | 33 | 207 ms | yes |
The page was run in Chromium (Playwright) with default bcrypt cost 10. Each line was read from the result block and checked independently of the page: a bcrypt compare, an apr1 recomputation with the salt from the line, and the known SHA1 value. The times include polling and automation overhead, so treat them as rough.
- bcrypt gave a 60-character hash starting with
$2y$10$at both 20 and 32 characters, and the compare confirmed it matches the password shown on the page. - apr1 has the form
$apr1$, an 8-character salt,$, then 22 characters. Recomputing it for “test” with the same salt gave the identical hash. - SHA1 for “test” produced exactly
{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=. The weakness warning was visible while SHA1 was selected, and it did not appear with bcrypt.

Use bcrypt for every new account. Choose apr1 or SHA1 only for compatibility with an older setup.
Privacy check: after typing a login and a password, local storage held neither value nor any hash, and no network request contained them.
Where to store .htpasswd and why HTTPS matters
Keep the .htpasswd file outside the public web root, for example in /home/example/secure/ rather than in public_html. Inside the root, a misconfigured server can serve it as a download, and anyone can then try to crack the hashes offline. The bcrypt line you paste is 66 characters in our test, and every such line in the file is a target if the file leaks. Restrict permissions so only the web server user (and you) can read it.
Basic auth does not encrypt anything. The browser sends the login and password base64-encoded, which anyone on the network path can decode. Serve the protected directory over HTTPS.
Common mistakes: the login prompt does not appear or keeps failing
- No prompt at all (.htaccess):
AllowOverrideis set toNone, so the file is ignored. SetAllowOverride AuthConfigfor that directory. - 500 error: the
AuthUserFilepath is wrong or relative, or mod_auth_basic or mod_authn_file is not loaded. The Apache error log names the problem. - Prompt appears but every login fails: the web server user can't read the file, the hash format isn't supported, the file holds a plain-text password instead of a hash, or the login or password has a typo. A colon in a hand-edited login also breaks the line. Regenerate the line and paste it again.
- Edited a Directory block, nothing changed: Apache has not been reloaded.
- Both requests return 200 in the curl check below: the rules aren't applied. Check for mixed 2.2 (
Order,Allow) and 2.4Requiredirectives.
Test that the protection works
Open the protected URL in a private window so no saved credentials interfere. You should see the browser's login prompt. Cancel it and you should get a 401 Unauthorized page.
From a terminal, check the status codes directly:
curl -I https://example.com/private/
curl -I -u alice:yourpassword https://example.com/private/
The first request should return 401, the second 200.
FAQ
How do I password protect a directory in Apache using .htaccess?
Put a .htpasswd file with a user:hash line outside the web root. Then create .htaccess in the target folder with AuthType Basic, AuthName, an absolute AuthUserFile path and Require valid-user. It only works if AllowOverride AuthConfig is enabled for that directory.
Where should the .htpasswd file be stored?
Outside the public web root, in a folder the web server user can read but visitors can't reach by URL. Reference it in AuthUserFile with its full absolute path.
How do I enable AllowOverride AuthConfig in Apache?
Add AllowOverride AuthConfig inside the <Directory> block that covers the folder in your virtual host or main config, then reload Apache. AllowOverride All also works but allows more than needed.
What is the difference between Apache 2.2 and 2.4 syntax for access control (Require valid-user)?
Apache 2.4 uses Require directives from mod_authz_core. Apache 2.2 used Order, Allow, Deny and Satisfy.
Do I need to restart Apache after enabling password protection?
Not for .htaccess. For a Directory block in the main config, a reload is enough; a full restart isn't required.
Ready to lock the folder? Build your line in the Htpasswd File Generator and paste it into your .htpasswd file.
Updated December 3, 2026
