HTTP 401 Unauthorized: What It Means and How to Fix It

You open a page or call an API and get 401 Unauthorized instead of the content. It’s one of the most common errors on the web — and one of the most misread, starting with its name. A 401 has nothing to do with permissions: it means the server doesn’t know who you are yet.

This guide covers what a 401 actually means, what the response looks like, every form you’ll see it in, what causes it, and how to fix it — as a visitor, as a site owner, and as a developer.

Quick answer

HTTP 401 Unauthorized means the request didn’t include valid credentials — no login, a wrong password, or an expired token. The server answers with a WWW-Authenticate header saying how to authenticate. Logging in or refreshing the token fixes it. If you’re already logged in and still blocked, that’s a 403 Forbidden, not a 401.

What HTTP 401 means

401 is the server saying “I don’t know who you are.” RFC 9110, the current HTTP specification, defines it as a request that “lacks valid authentication credentials” for the resource. If credentials were sent, the 401 means they were rejected.

Two things set it apart from every other 4xx code:

It always comes with instructions. A server that returns a 401 must send a WWW-Authenticate header naming the authentication method it accepts. That header is what makes a browser pop up a login box, and what tells an API client to fetch a new token.

It’s fixable by the client. Provide valid credentials and the same request succeeds. That’s the core difference from a 403, where the server refuses no matter who you are.

⚠️ The name is misleading. Despite “Unauthorized”, a 401 is about authentication — proving who you are — not authorization. The name dates back to HTTP/1.0 and was kept for compatibility. Our guide on 401 vs 403 explains the difference in detail.

What a 401 response looks like

For a site protected with a username and password — Basic authentication:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Restricted Area"
Content-Type: text/html; charset=utf-8

For an API that uses tokens — Bearer authentication:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="The access token expired"
Content-Type: application/json

{ "error": "invalid_token" }

The error field in the Bearer header is standardised: invalid_token means the token is missing, expired or revoked — a 401. A token that is valid but lacks permission gets insufficient_scope, which belongs with a 403.

Why the browser asks for a password

When a 401 carries WWW-Authenticate: Basic, the browser shows its own login box — the plain, unstyled prompt that isn’t part of the site. Cancel it, and you see the server’s 401 page instead. Bearer tokens never trigger that box: they’re handled by the application’s code, which is why APIs and single-page apps show their own login screens instead.

⚠️ Basic authentication only encodes the password — it doesn’t encrypt it. The credentials travel in Base64, which anyone can decode. Over plain HTTP they’re exposed on the network; protected areas should always be served over HTTPS.

Every way you’ll see a 401

The same code shows up with different wording depending on the server and the tool:

What you seeWhereWhat it means
401 UnauthorizedBrowsers, logs, API responsesThe standard form
“This server could not verify that you are authorized to access the document requested”Apache’s default pageBasic authentication failed or the prompt was cancelled
HTTP Error 401.1 – Unauthorized (also 401.2, 401.3)Microsoft IISIIS sub-codes: login failed, a server configuration problem, or file permissions on the resource
rest_not_logged_inWordPress REST APIThe request to the API wasn’t authenticated
invalid_tokenOAuth and JWT-based APIsThe token is missing, expired or revoked
“Blocked due to unauthorized request (401)”Google Search ConsoleGooglebot hit a page that requires a login
407 Proxy Authentication RequiredCorporate networksThe same idea, but the proxy wants credentials, not the website

What causes a 401

No credentials were sent. The page or endpoint requires a login and the request didn’t include one — the most common case of all.

The credentials are wrong. A mistyped password, a username with an extra space, or an API key copied incompletely.

The session or token expired. Logins and tokens have a lifetime; after it ends, the next request returns a 401 until you authenticate again.

The credentials never reached the application. The request included them, but something on the way stripped the Authorization header — a proxy, a CDN, or the web server itself. This one is especially common on shared hosting, and it’s covered below.

The wrong authentication method. The server expects a Bearer token and received Basic credentials, or the other way round.

How to fix a 401 as a visitor

  1. Log in again. Re-enter your username and password carefully — the most common cause is simply an expired session.
  2. Check the URL. Make sure you’re on the right address; a login protecting one area doesn’t apply to another.
  3. Clear the site’s cookies, or open a private window. A corrupted or stale session cookie can make the server reject a login that should work.
  4. Reset the password if the site accepts neither your old nor your saved credentials.
  5. Contact the site owner if you’re sure the credentials are right. The problem may be on the server side — see the next section.

How to fix a 401 as a site owner or developer

Confirm what the server actually receives

Test outside the browser, so no cached session gets in the way:

curl -i -u username:password https://example.com/protected/
curl -i -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/me

If curl succeeds and the browser fails, the problem is on the client side — cookies, cached credentials, or the app’s token handling. If curl fails too, look at the server.

The Authorization header is being stripped

This is the 401 that confuses developers most on shared hosting. Your code sends a valid token, the server logs show the request — and the application still says it received no credentials.

The usual cause: on Apache with PHP running through FastCGI or PHP-FPM, the Authorization header isn’t passed to PHP by default. The request arrives with the token; your code never sees it. Add this to the .htaccess file:

CGIPassAuth On

On older Apache versions that don’t support that directive, this line does the same job:

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

It’s the fix for most “401 on my API, but only on the server” reports — including JWT authentication in WordPress and Laravel.

The token expired — refresh it once, don’t loop

An app that retries a request with the same expired token gets the same 401 forever. Worse, if the refresh endpoint itself returns 401 and the app answers by refreshing again, it loops until the browser gives up.

The rule that prevents both: when a request returns 401, refresh the token once and repeat the request. If the refresh also fails, stop and send the user to the login screen.

A CORS preflight returning 401

Before a cross-origin request with custom headers, the browser sends an OPTIONS preflight — without credentials. If your API demands authentication on every method, the preflight gets a 401, and the browser reports a CORS error instead, never sending the real request.

The fix is to let OPTIONS requests through without authentication and answer them with the CORS headers. The real request that follows carries the token as usual.

The WordPress REST API returns 401

A rest_not_logged_in response usually has one of three causes: the request wasn’t authenticated at all — external tools need an Application Password, created in the user’s profile; the host is stripping the Authorization header, which the .htaccess fix above solves; or a security plugin is blocking the REST API for unauthenticated users.

How to protect a page with a 401 on purpose

A 401 isn’t only an error — it’s also how you password-protect a folder. On Apache, add to the folder’s .htaccess:

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

Create the password file with htpasswd -c /home/user/.htpasswd username. On Nginx, the equivalent is auth_basic with auth_basic_user_file. Most control panels offer the same thing as Directory Privacy or Password Protect Directories, through a form. To show visitors a friendlier page when they cancel the login, set up a custom error page for the 401.

Does a 401 affect SEO?

Googlebot never logs in, so anything behind a 401 isn’t indexed. That’s exactly what you want for staging sites and private areas — a password-protected staging copy is safer than one hidden with noindex.

It becomes a problem when public pages start returning 401 — usually after a security plugin, a misconfigured protected directory or a firewall rule catches them. Search Console reports them as “Blocked due to unauthorized request (401)”, and they drop out of the index until the protection is removed. For the full picture of how Google treats each code, see our HTTP status code reference.

401 vs 403

The two are easy to mix up because of the names. 401: the server doesn’t know who you are — logging in fixes it. 403: the server refuses you whoever you are — logging in doesn’t help. A logged-in user without permission gets a 403, not a 401. Our 401 vs 403 guide compares them side by side, including how to return each one from an API.

Frequently asked questions

What does HTTP 401 Unauthorized mean?
The request didn’t include valid authentication credentials — no login, wrong credentials, or an expired token. The server responds with a WWW-Authenticate header saying how to authenticate, and providing valid credentials makes the same request succeed.

Is a 401 error my fault or the website’s?
Usually it’s on the client side: a missing login, a mistyped password or an expired session. But if your credentials are correct and the error persists, the cause may be on the server — a misconfigured protected directory, or a server that strips the credentials before they reach the application.

How do I fix a 401 error?
Log in again, check the URL, clear the site’s cookies or try a private window, and reset your password if needed. If none of that works, contact the site owner — the problem may be in the server’s configuration.

Why do I get a 401 even with the right password?
On a site, a stale session cookie can make the server reject a valid login — clearing cookies usually fixes it. In an API on Apache with PHP-FPM, the server may be stripping the Authorization header, so the application never receives the credentials; CGIPassAuth On in .htaccess solves it.

What’s the difference between 401 and 403?
401 means the server doesn’t know who you are, and logging in fixes it. 403 means the server refuses the request whoever you are, and logging in doesn’t help. A logged-in user without permission gets a 403.

What’s the difference between 401 and 407?
Both ask for credentials, but for different targets. A 401 comes from the website you’re trying to reach; a 407 Proxy Authentication Required comes from a proxy between you and the site, usually on a corporate network.

Why does my API keep returning 401 in a loop?
The app is retrying with an expired token, or refreshing the token through an endpoint that itself returns 401. Refresh the token once when a 401 arrives; if the refresh fails, stop and send the user to log in again.

Does a 401 hurt SEO?
Only if it’s on pages that should be public. Googlebot can’t log in, so anything behind a 401 isn’t indexed — which is correct for staging sites and private areas. Public pages returning 401 appear in Search Console as “Blocked due to unauthorized request (401)” and drop from the index.

Conclusion

A 401 is the server asking a question, not closing a door: “who are you?” Answer it with valid credentials and the same request goes through. That’s what separates it from a 403, where the answer doesn’t matter.

For visitors, the fix is almost always to log in again or clear a stale session. For site owners and developers, the cases worth knowing are the ones that look impossible — a valid token rejected because the server stripped the Authorization header, an app looping on an expired token, a CORS preflight blocked before the real request is sent.

And when the 401 is intentional — a staging site, a private folder — it’s doing exactly its job: keeping visitors and search engines out until they prove who they are.

Share the Post:
Picture of Gustavo Gallas

Gustavo Gallas

Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development​. Also have brewing skills.