Skip to main content

WP 301 Redirects

Use 401 when the user is not logged in, or their login token is missing or broken. Use 403 when the user is logged in, but still cannot access the thing they asked for. That is the main rule. It saves hours of confused debugging.

TLDR: 401 means “Who are you?” 403 means “I know who you are, and still no.” For example, if 1,000 users hit an admin page and 72% are not logged in, those requests should get 401. If 15 logged-in users are regular members trying to open the admin panel, they should get 403.

401 vs 403 in one silly example

Imagine a fancy club.

You walk up to the door. The bouncer says, “Show me your ID.” You forgot it. That is a 401 Unauthorized.

Now imagine you do show your ID. The bouncer reads your name. Then he says, “You are not on the VIP list.” That is a 403 Forbidden.

Annoying? Yes. Clear? Also yes.

Image not found in postmeta

The weird name: why 401 is called “Unauthorized”

This is where HTTP gets a little rude.

401 Unauthorized sounds like it means “you do not have permission.” But in real use, it usually means you are not authenticated.

In plain English:

  • Authentication asks, “Who are you?”
  • Authorization asks, “Are you allowed to do this?”

A 401 is mostly about authentication. The server does not trust the request yet. Maybe there is no API key. Maybe the session cookie expired. Maybe the bearer token is bad. Maybe the user never logged in at all.

The catch is that the name says Unauthorized, so people mix it up with 403 Forbidden. It drives me crazy that one word has caused so many support tickets.

What 401 really means

401 Unauthorized means the request needs valid credentials.

The server is saying:

“I might let you in, but first prove who you are.”

Common causes include:

  • No login session.
  • Missing Authorization header.
  • Expired token.
  • Bad password.
  • Invalid API key.
  • Cookie blocked by the browser.
  • Token sent to the wrong domain.

A proper 401 response often includes a WWW-Authenticate header. This tells the client how to authenticate. For example, an API may say it expects a bearer token.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api"

That header is like the server saying, “Try again, but bring the right badge.”

What 403 really means

403 Forbidden means the server understood the request. It may even know the user. But access is still denied.

The server is saying:

“I know who you are. You still cannot do this.”

Common causes include:

  • A normal user tries to open an admin-only page.
  • A free plan user tries to use a paid feature.
  • An employee tries to view another department’s data.
  • An account is suspended.
  • An IP address is blocked.
  • A file exists, but this user cannot read it.

With 403, logging in again usually does not fix the issue. The user needs a different role, plan, status, or permission.

Image not found in postmeta

A simple decision rule

Ask one question first:

Can the server identify the user?

  • If no, return 401.
  • If yes, ask the next question.

Second question:

Is this user allowed to access the resource?

  • If yes, return 200 OK or the right success code.
  • If no, return 403.

That is it. Two questions. Fewer headaches.

Quick examples for websites

Example 1: private dashboard

A user opens /dashboard with no session cookie. Return 401. They need to log in.

Example 2: admin settings

A logged-in user opens /admin/settings. They are not an admin. Return 403. They are known, but blocked.

Example 3: deleted account

A banned user logs in and tries to post a comment. Return 403. Their identity is known. The action is not allowed.

Example 4: expired session

A user clicks “Save” after leaving a tab open for 9 hours. Their session expired. Return 401. Ask them to log in again.

Quick examples for APIs

APIs need clear status codes. Machines read them. Humans swear at them later.

Missing token:

GET /api/orders
Authorization: none
Response: 401 Unauthorized

The API has no proof of identity.

Bad token:

GET /api/orders
Authorization: Bearer broken.token.here
Response: 401 Unauthorized

The token failed. Try again with a valid one.

Valid token, wrong role:

GET /api/admin/users
Authorization: Bearer valid.member.token
Response: 403 Forbidden

The user is real. The role is wrong.

Valid token, wrong account:

GET /api/accounts/999/invoices
Authorization: Bearer valid.token.for.account.123
Response: 403 Forbidden

The user belongs to account 123, not account 999.

Why using the wrong code hurts

Wrong status codes cause tiny disasters.

If you return 403 for a missing login, the app may not show the login screen. It may show a scary “access denied” page instead.

If you return 401 for a user with the wrong role, the app may keep asking them to log in. Again. And again. Honestly, it feels like the software is gaslighting them.

Expect to waste time on bugs like:

  • Mobile apps stuck in login loops.
  • Support teams getting vague access complaints.
  • Analytics showing fake “permission” problems.
  • Users resetting passwords for no reason.
  • API clients retrying requests that will never work.

One wrong code can add seconds to every failed request flow. Multiply that by thousands of users. Now it is not tiny anymore.

Should you ever return 404 instead?

Sometimes, yes.

A 404 Not Found can hide whether a resource exists. This is useful for security.

Say someone tries this:

GET /api/users/8421/private-notes

If you return 403, you may reveal that user 8421 exists. If that is sensitive, return 404 instead.

This is common for private files, user profiles, internal records, and secret projects.

Use this carefully. Do not use 404 just because the permission system is messy. That only hides the mess from users. It does not hide it from your future self.

Image not found in postmeta

Best practices for friendly errors

Status codes are for browsers, apps, and APIs. Error messages are for people.

Make them helpful.

  • For 401, say: “Please log in to continue.”
  • For 403, say: “You do not have permission to view this page.”
  • For expired sessions, say: “Your session expired. Please log in again.”
  • For plan limits, say: “This feature needs a Pro plan.”

Do not expose private details. Avoid messages like, “User exists, but lacks billing admin role.” That may be useful to attackers.

For APIs, include a simple error body:

{
  "error": "forbidden",
  "message": "You do not have permission to access this resource."
}

Keep it dull. Dull is good. Dull is safe.

The cheat sheet

  • 401 Unauthorized: Not logged in, missing token, bad token, expired session.
  • 403 Forbidden: Logged in, known user, not enough permission.
  • 404 Not Found: Resource does not exist, or you want to hide that it exists.

Here is the shortest memory trick:

401 = bring your ID.

403 = your ID is valid, but you are not on the list.

Use that rule, and your error handling will feel cleaner. Your users will get better messages. Your API clients will behave better. And your support team may stop asking why everyone is “forbidden” from a page they simply forgot to log into.