Lab-tested: Apache 2.4.71 (Debian) · 2 Dec 2026 · Reproduced and fixed in our lab
Fix Box — Try This First (10 Seconds)
An Apache 401 almost always comes from its own auth modules, not your app. Read the WWW-Authenticate header first — it names the scheme (Basic or Digest) and the realm Apache expects.
Test with curl and known-good credentials before touching config. If curl succeeds but your script still sees no user, suspect CGIPassAuth.
Start from the pillar 401 Unauthorized: Causes and How to Fix It or run the 401 Checker against your URL.
Table of Contents
- How Apache Decides to Return 401
- The Exact Responses (Verbatim from Our Lab)
- Cause 1: No Credentials or Wrong Password (Basic)
- Cause 2: Digest Challenge Mismatch
- Cause 3: Wrong Scheme Sent to the Endpoint
- Cause 4: CGI/FastCGI Never Sees the Authorization Header
- Cause 5: .htaccess Auth Silently Ignored
- Our Lab Reproduction
- The Fix, Step by Step
- What Did Not Work
- Prevention
- FAQ
- Sources and Date
- References
How Apache Decides to Return 401
Apache authenticates before your content runs. mod_auth_basic and mod_auth_digest check the Authorization header against a password file (mod_authn_file), and mod_authz_user applies the Require rule.
If credentials are missing or refused, Apache answers 401 itself and your script never executes.
That ordering matters for diagnosis. A 401 with a Basic or Digest challenge and an Apache-style error page means the web server layer refused you.
A 401 with a JSON body usually comes from the application behind Apache. The header tells you which layer to fix.
The Exact Responses (Verbatim from Our Lab)
We ran Apache 2.4.71 on Debian on 2 Dec 2026 with Basic, Digest, .htaccess, and CGI locations. These captures are verbatim from that run:
# No credentials, Basic location
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Lab Basic"
# No credentials, Digest location
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Digest realm="Lab Digest", nonce="6oA6CZ1hBgA=...", algorithm=MD5, domain="/digest/", qop="auth"
# Correct Basic credentials
HTTP/1.1 200 OK
lab basic protected content
The realm string is whatever you set in AuthName. Browsers show it in the login prompt.
If the realm in the response is not the one you configured, a different config block is answering — check for overlapping <Location> or <Directory> rules.
Cause 1: No Credentials or Wrong Password (Basic)
This is the plain case. No Authorization header, a wrong password, or an unknown user all produced the identical 401 in our lab. Apache deliberately does not distinguish them in the response.
Check the password file first. htpasswd entries are hashed, so verify by testing, not by reading: create a test user and try it. Also confirm AuthUserFile points at the file you think it does — a stale path to an old file is a classic cause of "correct password rejected."
Cause 2: Digest Challenge Mismatch
Digest is a challenge-response scheme. The first request always gets a 401 carrying a nonce; the client must answer with a hashed response.
A client that sends Basic credentials to a Digest location, or replays an old nonce, gets 401 again.
In our lab, curl --digest completed the two-step exchange and returned 200. Plain curl -u (Basic) against the Digest location did not.
If your client library cannot do Digest, switch the location to Basic over HTTPS instead of debugging the client.
Cause 3: Wrong Scheme Sent to the Endpoint
Sending Authorization: Bearer <token> to an Apache Basic location returned 401 in our lab, with a fresh Basic challenge. Apache ignores schemes it did not configure.
API clients that default to Bearer hit this constantly when an endpoint sits behind Apache Basic auth.
The fix is on the client: match the scheme in the challenge. Read WWW-Authenticate and send that scheme. For the client-side patterns, see 401 JWT/Bearer Token Errors and 401 Basic Auth Failures.
Cause 4: CGI/FastCGI Never Sees the Authorization Header
This is the trap that wastes the most time. By default, Apache hides the Authorization header from CGI and FastCGI scripts.
The script receives REMOTE_USER but not HTTP_AUTHORIZATION, so app-level auth checks inside the script fail as if no credentials were sent.
Our lab proved both states. With the default, the authenticated CGI request returned REMOTE_USER: labuser and HTTP_AUTHORIZATION: (absent).
With CGIPassAuth On, the same request returned HTTP_AUTHORIZATION: present. The directive exists since Apache 2.4.13 and defaults to Off.
<Directory "/var/www/cgi-bin">
Options +ExecCGI
CGIPassAuth On
</Directory>
Only enable it when the script itself must read credentials. Passing passwords to scripts widens the blast radius if the script logs its environment.
Cause 5: .htaccess Auth Silently Ignored
An .htaccess file with AuthType Basic only works when the directory allows it. With AllowOverride AuthConfig, our lab location returned 401 without credentials, as expected.
With AllowOverride None, the identical .htaccess was ignored and the content was served with 200 and no credentials at all.
That failure mode is silent and dangerous: the folder looks protected in the repo but is open in production. The reverse also happens — auth rules in .htaccess "not working" after a server migration because AllowOverride changed.
Always test the protected URL without credentials after any config change.
Our Lab Reproduction
Stack: Apache 2.4.71 (Debian) on localhost, tested 2 Dec 2026. We configured Basic and Digest locations, an .htaccess-protected directory under both AllowOverride settings, CGI scripts with and without CGIPassAuth, and an IP-restricted location.
Twelve cases ran: every missing/wrong-credential case returned 401 with the expected challenge, every correct case returned 200, and the IP-restricted location returned 403.
Honest lab note: this was a single-server lab without a reverse proxy in front. In production, a proxy that strips Authorization produces the same symptom as Cause 4 with a different fix location.
The diagnostic order still applies: challenge first, credentials second, header visibility third.
The Fix, Step by Step
- Reproduce with curl and read the challenge:
curl -si https://your-site.example/protected/
curl -si -u testuser:'test-password' https://your-site.example/protected/
- Match the scheme. If the challenge says
Basic, send Basic. If it saysDigest, use a Digest-capable client or change the server config. - Verify the password file and user:
htpasswd -v /path/to/.htpasswd testuser
- If the script sees no credentials, check header visibility. Add
CGIPassAuth Ononly to the directory that needs it, then retest. - If
.htaccessauth seems inactive, checkAllowOverridefor that directory in the server config. Confirm with a no-credentials request: protected means 401. - Reload and retest all three states: no credentials (401), wrong credentials (401), correct credentials (200).
apachectl configtest && apachectl graceful
What Did Not Work
Sending a Bearer token to the Basic location did not work, and no token format fixes that. Apache only speaks the schemes its auth modules were configured with. The client must change, or the server config must.
Rewriting the Authorization header into a custom environment variable with mod_rewrite is the pre-2.4.13 workaround for Cause 4. It works, but CGIPassAuth On is the supported switch on any modern Apache. Prefer the directive.
Editing .htaccess while AllowOverride None is set changed nothing in our lab — the file was never read. When auth edits have no effect at all, check whether the file is being honored before debugging its contents.
Prevention
Keep auth rules in the server config where possible; use .htaccess only when you cannot. Test every protected URL in the three credential states after each deploy. Log 401s by realm so you can see which location is refusing traffic.
For the wider picture, see the pillar 401 Unauthorized: Causes and How to Fix It, the 401 Checker, 401 Basic Auth Failures, and Nginx 401 Unauthorized for the other common server.
FAQ
Why does Apache return 401 even with the correct password? Usually the AuthUserFile path points at a different file than the one you updated, or a stricter config block wins.
Verify with htpasswd -v against the exact file in the active config, and check the realm in the response matches your AuthName.
What is the difference between Basic and Digest in Apache? Basic sends base64-encoded credentials on every request; Digest answers a nonce challenge with a hash, so the password itself never crosses the wire.
Digest needs a client that supports it and an htdigest password file.
Why is HTTP_AUTHORIZATION empty in my PHP/CGI script? Apache hides it by default.
Set CGIPassAuth On for that directory (Apache 2.4.13+). The setting is respected by CGI, FastCGI, and proxy modules that use the standard CGI environment.
My .htaccess password protection stopped working. Why? The directory's AllowOverride likely changed to None or lost AuthConfig, so Apache ignores the file.
In our lab that served the content with 200 and no login at all. Fix AllowOverride in the server config and retest without credentials.
Can Apache return 401 for a Bearer token API? Not with the stock auth modules.
Apache's built-in schemes are Basic, Digest, and Form. Bearer APIs usually authenticate in the application; if Apache sits in front, make sure it does not strip the header before the app sees it.
Sources and Date
Lab tests run 2 Dec 2026 on Apache 2.4.71 (Debian), localhost, twelve cases. Captures are verbatim from that run with the lab password masked. References verified live on 2 Dec 2026.
References
- RFC 9110, HTTP Semantics, Section 15.5.2 (401 Unauthorized): https://www.rfc-editor.org/rfc/rfc9110.html
- Apache HTTP Server Documentation, mod_auth_basic: https://httpd.apache.org/docs/2.4/mod/mod_auth_basic.html
- Apache HTTP Server Documentation, mod_auth_digest: https://httpd.apache.org/docs/2.4/mod/mod_auth_digest.html
- Apache HTTP Server Documentation, CGIPassAuth (core): https://httpd.apache.org/docs/2.4/mod/core.html#cgipassauth
- RFC 7617, The 'Basic' HTTP Authentication Scheme: https://www.rfc-editor.org/rfc/rfc7617.html
- RFC 7616, HTTP Digest Access Authentication: https://www.rfc-editor.org/rfc/rfc7616.html






Leave a Reply