Skip to content

Password-protecting a directory on cPanel with Directory Privacy

How to put HTTP basic-auth on a folder using cPanel's Directory Privacy or .htaccess + .htpasswd, and how to fix the 500s and login loops that catch people.

Putting a password prompt in front of a folder — a staging site, an admin area, a downloads directory — is HTTP basic authentication, and cPanel exposes it through the Directory Privacy tool. Under the hood it writes an .htaccess that points at an .htpasswd credentials file, and Apache challenges every request into that folder. The mechanism is simple, but a few details routinely cause 500 errors or a login box that never accepts the password: a wrong path to .htpasswd, a hashing mismatch, or interaction with an application’s own auth. The goal is a folder that returns a 401 challenge to anonymous visitors and serves normally once authenticated.

What’s happening

Basic auth on Apache needs two files. An .htaccess in the protected folder declares the realm and the location of the credentials file (AuthType Basic, AuthName, AuthUserFile /full/path/.htpasswd, Require valid-user). The .htpasswd file holds username:hashed-password lines. When anyone requests a file in that folder, Apache returns 401 Unauthorized with a WWW-Authenticate header, the browser shows the login box, and Apache checks the submitted credentials against .htpasswd.

cPanel’s Directory Privacy tool writes both files correctly and puts .htpasswd outside the web root by default, so most people should use it rather than hand-editing. The failures come from doing it by hand or from edge cases: an AuthUserFile with a relative or wrong absolute path (Apache cannot read it → 500), a .htpasswd created with a hash Apache does not accept, the .htpasswd file itself being web-accessible (a leak), or basic auth layered on a folder that the application also protects, producing a double prompt or an Authorization header the app strips. AllowOverride must also permit AuthConfig for the .htaccess to take effect.

How to verify

Confirm the two files exist, their paths agree, and Apache can read the credentials file.

# The .htaccess in the protected folder and where it points
cat /home/<cpaneluser>/public_html/<folder>/.htaccess
grep -i 'AuthUserFile' /home/<cpaneluser>/public_html/<folder>/.htaccess

# Does that .htpasswd path actually exist and is it readable?
ls -la /home/<cpaneluser>/.htpasswds/...      # cPanel stores them here
namei -l /full/path/to/.htpasswd

Confirm Apache challenges anonymous requests and accepts the credentials.

# Anonymous request should be 401
curl -sI https://example.com/<folder>/ | head -1

# With credentials it should be 200
curl -sI -u '<user>:<pass>' https://example.com/<folder>/ | head -1

# The error log names auth/path failures
tail -20 /home/<cpaneluser>/logs/<domain>.error.log
#   .htpasswd: cannot open / Permission denied  -> bad path or perms
#   AuthConfig not allowed here                 -> AllowOverride issue

The fix

  1. Use cPanel’s Directory Privacy tool — cPanel » Files » Directory Privacy lets you tick “Password protect this directory,” set the realm name, and add users with passwords. cPanel writes a correct .htaccess and stores .htpasswd outside the web root with the right hash. For nearly every case this is the whole job and avoids the hand-edit pitfalls below.
  2. Fix a wrong AuthUserFile path — a 500 with .htpasswd: cannot open means the path in .htaccess is wrong or relative. AuthUserFile must be an absolute path to a file Apache can read (e.g. /home/<cpaneluser>/.htpasswds/public_html/<folder>/passwd). Correct the path and reload; this is the most common hand-rolled basic-auth failure.
  3. Create .htpasswd with an accepted hash — if the login never accepts the password, the hash may be one Apache rejects. Generate it with htpasswd -B -c /path/.htpasswd <user> (bcrypt) or let cPanel create it. Do not paste plaintext or an unsupported hash into the file; the username must match exactly.
  4. Keep .htpasswd out of the web root — if .htpasswd sits inside public_html, the hashes can be fetched over the web. Place it above the docroot (cPanel does this automatically), or deny access to it with a <Files .htpasswd> Require all denied </Files> block. A readable credentials file is a real exposure even though the hashes are hashed.
  5. Ensure AllowOverride permits AuthConfig — AuthConfig not allowed here in the log means the account’s AllowOverride does not allow auth directives, so the .htaccess is ignored and the folder stays open. Use the Directory Privacy tool (which works regardless), or have AllowOverride AuthConfig set for the account (see the AllowOverride reference).
  6. Avoid double auth with the application — if the folder is also protected by the app (a CMS admin login), basic auth on top causes a confusing double prompt, and some PHP setups strip the Authorization header so the app cannot see its own credentials. Protect at one layer, or add SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0 / a CGIPassAuth On so the app still receives the header when both are genuinely needed.
  7. Scope the protection to the intended folder — placing the .htaccess too high protects more than you meant (it cascades to all subfolders); too low leaves siblings open. Put it in exactly the folder you want challenged, and test a file both inside and just outside it.

After setting it up, confirm curl -sI returns 401 for an anonymous request and 200 with -u user:pass — that pair of responses is the definitive proof the folder is protected and the credentials work, rather than trusting a login box that may be cached. Stack Harbor handles cPanel access-control and basic-auth setup under WHM server management — start a conversation.