How to Password Protect a Site or Folder With .htpasswd

Set up HTTP Basic Authentication on Apache or Nginx with an .htpasswd file, choose bcrypt over legacy hashes, and keep staging sites from being exposed.

Updated September 29, 2026 6 min read

Use the free .htpasswd GeneratorCreate Apache .htpasswd entries with bcrypt or SHA-1 for many users

You have a staging copy of a website that clients need to review, an admin directory that should not be public, or an internal dashboard on a small server. You do not want to build a login system for it; you just want a password prompt in front of it. HTTP Basic Authentication, configured with an .htpasswd file, does exactly that, and both Apache and Nginx support it out of the box.

This guide explains how Basic Authentication works, what goes into an .htpasswd file, how to configure Apache and Nginx, which hash format to pick, and the security caveats you should know before relying on it.

How HTTP Basic Authentication works

When a browser requests a protected page without credentials, the server responds with status 401 Unauthorized and a WWW-Authenticate: Basic realm="..." header. The browser shows its built-in login dialog. After you type a username and password, the browser sends them with every request in a header:

Authorization: Basic YWxpY2U6U3ByaW5nLTIwMjY=

That long value is not encrypted. It is just username:password encoded in Base64, which anyone can reverse:

Buffer.from("YWxpY2U6U3ByaW5nLTIwMjY=", "base64").toString(); // 'alice:Spring-2026'

The practical consequence: only use Basic Authentication over HTTPS, where the whole request, headers included, is encrypted in transit. Over plain HTTP, anyone on the network path can read the password.

The server checks the credentials against the .htpasswd file, which stores usernames with hashed passwords, so the file itself does not reveal the passwords directly.

What an .htpasswd file contains

One line per user, in the form username:hash:

alice:$2y$10$...
bob:$2y$10$...

The hash format determines how safe the file is if it leaks:

Format Prefix Notes
bcrypt $2y$ Salted and deliberately slow. The recommended choice for Apache 2.4.
Apache MD5 $apr1$ Salted, but fast to attack. Long the default of the htpasswd tool.
SHA-1 {SHA} Unsalted and fast. Only for very old setups.
crypt none Old Unix crypt, limited to 8 characters of password. Avoid.

Nginx reads the same file format. It understands {SHA} and $apr1$ itself, and other formats such as bcrypt through the operating system's crypt() function, so bcrypt support depends on the system library. If Nginx rejects bcrypt entries on your server, check your distribution's documentation or use $apr1$ as a fallback.

Create the file with our tool

If you have shell access, the htpasswd command (see below) is the natural choice. On shared hosting, or when you would rather not install Apache utilities, the .htpasswd Generator creates the file in your browser.

  1. Open the .htpasswd Generator.
  2. Enter a username and password for the first user. Click the random password button to generate a strong 16-character password, and Add user for more rows. Usernames cannot contain colons or spaces, and duplicates are flagged.
  3. Choose the Hash algorithm: bcrypt ($2y$), recommended, or SHA-1 ({SHA}), legacy for old servers. For bcrypt, set the Cost factor (the default of 10 is a sensible start).
  4. Click Generate .htpasswd, then copy or Download the .htpasswd contents.
  5. Enter the Full server path of the .htpasswd file, and copy the ready-made .htaccess snippet with Copy .htaccess.

Hashing happens locally, so the passwords never leave your browser. Store the plain-text passwords in your password manager; the file only contains hashes.

Configure Apache

Upload the .htpasswd file to a location outside the public web root, for example /home/account/.htpasswd rather than /home/account/public_html/.htpasswd. Then place this in an .htaccess file inside the directory you want to protect, or in the server configuration:

AuthType Basic
AuthName "Restricted area"
AuthUserFile /home/account/.htpasswd
Require valid-user

AuthUserFile needs the full filesystem path, not a URL. AuthName is the realm shown in some browsers' login dialogs. Require valid-user admits anyone listed in the file; Require user alice would admit only that user.

For .htaccess directives to work, the server must allow them with AllowOverride AuthConfig (or All) for that directory. Most shared hosts allow it already.

Configure Nginx

Nginx does not use .htaccess files. Add the directives to the relevant server or location block:

location / {
    auth_basic "Restricted area";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Reload Nginx after saving (nginx -t to test the configuration, then reload it with your system's service manager). Make sure the Nginx worker user can read the file.

Using the command line instead

The htpasswd utility ships with Apache (on Debian and Ubuntu in the apache2-utils package):

# Create a new file with a bcrypt entry (-c creates, -B selects bcrypt, -C sets the cost)
htpasswd -c -B -C 12 /etc/nginx/.htpasswd alice

# Add another user to the existing file (no -c, or you overwrite it)
htpasswd -B /etc/nginx/.htpasswd bob

The most common accident is re-running with -c, which replaces the file and removes every other user. Without htpasswd, openssl passwd -apr1 produces an $apr1$ hash you can paste after username:.

Security caveats

It is a gate, not an account system

Basic Authentication has no logout button, no lockout after failed attempts, no password reset and no two-factor authentication. Browsers cache the credentials until they are closed. For anything with real users or sensitive data, use proper application authentication.

Rate limiting matters

Attackers can try passwords as fast as the server responds. Use long random passwords, and consider tools such as rate limiting or fail2ban on public servers.

Keep staging sites out of search results

Password protection normally keeps crawlers out, since they receive a 401. But if protection is ever removed, a staging site can be indexed and compete with the live one. Add a noindex header or robots rules as a second layer; the robots.txt Generator helps with the latter.

Mind the file permissions

The file should be readable by the web server and not writable by it, and it should never be downloadable. Apache's default configuration usually denies requests for files starting with .ht, but keeping the file outside the web root removes the question entirely.

For redirects in the same .htaccess file, use the .htaccess Redirect Generator. To understand the bcrypt hashes in your file, or to verify a password against one, use the Bcrypt Hash Generator.

Frequently asked questions

Is HTTP Basic Authentication secure?

Over HTTPS, with strong passwords and bcrypt hashes, it is reasonable for staging sites and internal tools. Over plain HTTP it is not, because the credentials are only Base64-encoded.

Where should I put the .htpasswd file?

Outside the public web root, in a directory the web server can read. Reference it with its full filesystem path in AuthUserFile or auth_basic_user_file.

Does Nginx support bcrypt in .htpasswd files?

Nginx passes unknown hash formats to the system's crypt() function, so bcrypt works when the operating system library supports it. If it does not, use the $apr1$ format.

How do I log out of Basic Authentication?

There is no standard logout. Closing the browser clears the cached credentials; some browsers also forget them when you clear site data.

Can I protect only one file instead of a folder?

Yes. In Apache, wrap the directives in a <Files "report.pdf"> block; in Nginx, use a location block that matches that path.

Try it now: .htpasswd Generator

Loading tool…