LOGBOOK

HELP

Quiz Entry - updated: 2026.11.05

What do .htaccess and htpasswd do for Apache Basic Authentication, and what must be true for a deployed .htaccess to take effect?

.htaccess in the web root tells Apache to require a login (AuthType Basic, Require valid-user) and where the password file is (AuthUserFile). htpasswd stores the user names and password hashes. Apache only reads .htaccess if AllowOverride permits it for that directory, and its worker user must be able to read the password file.

Request flow: AllowOverride decides whether .htaccess is read at all, then the web server user must be able to read htpasswd; otherwise the site is open or logins fail with 500

* Two checks decide whether the login really protects the site. *

AuthType Basic
AuthName "Protected area - please enter a password"
Require valid-user
AuthUserFile /etc/httpd/secrets/htpasswd
guest:$2y$05$MQ7RdCCRrKmBEDD/u4pks.9qAV8RCKy4YCP2pzgmc4lppfli41zC

The hash prefix $2y$ means bcrypt (made with htpasswd -B), and 05 is its cost factor. The file is kept outside the web root so it can never be downloaded.

Two conditions are easy to get wrong when automating this:

  1. AllowOverride. On Debian, /var/www/ defaults to AllowOverride None, so Apache ignores .htaccess entirely and the site stays open. The virtual host or directory config must allow at least AllowOverride AuthConfig. (Putting the Auth* lines straight into the <Directory> block avoids .htaccess altogether.)
  2. Readability. Apache's workers run as an unprivileged user (www-data on Debian, apache on RHEL). If the secrets directory is root:root 0500 and htpasswd is root:root 0400, the workers can't read them, and every login fails with a server error. Give the files to the web server's group, or make them group-readable for it.

So a check like "the URL returns HTTP 200 with the right credentials" only proves the auth works if a request without credentials gets a 401.

Go deeper:

From Quiz: ITIA / Ansible Variables, Secrets and Facts | Updated: Nov 05, 2026