Complete Guide to HTTP Status Codes
Every time you visit a website, your browser and the server have a quick, invisible conversation. The server replies with a three-digit number known as an HTTP status code. Think of these codes as a short note attached to a package. They tell you if the delivery was successful (200 OK), if the address changed (301 Redirect), or if something went wrong along the way.
Most of the time, you won't see these codes because the page just loads. But when a website crashes or a link breaks, knowing these numbers is the only way to figure out why it happened and how to fix it.
The 5 Types of Response Codes (Cheat Sheet)
The first digit of the code tells you who is responsible for the result. Here is the easiest way to remember them:
- 1xx (Info): "Hold on." The server received the request and is still processing it. You almost never see these in a web browser.
- 2xx (Success): "Here you go." The request worked perfectly. The server sent the data you asked for.
- 3xx (Redirect): "Go here instead." The file has moved to a new address. Your browser will automatically take you to the new URL.
- 4xx (Client Error): "You messed up." This is usually a user error. It means you clicked a broken link, typed a URL wrong, or don't have permission to view the page.
- 5xx (Server Error): "I messed up." This is a server error. The website code crashed, the database is down, or the server is overloaded.
Do Status Codes Affect SEO?
Yes, they are huge for SEO. Search engines like Google use these codes to decide how to rank your site.
- 200 OK: Tells Google "This page is good, index it."
- 301 Redirect: Tells Google "We moved this page forever. Please pass all our ranking power to the new URL."
- 404 Not Found: If Google sees too many of these, it might stop visiting your site as often (wasting your "crawl budget").
- 500 Errors: These are dangerous. If Google sees a 500 error consistently, it will drop the page from search results quickly because it thinks the site is broken.
Best Status Codes for REST APIs
If you are building a REST API, you don't need to use every code. Stick to these standard responses:
- GET (Read): 200 OK (Success) or 404 Not Found.
- POST (Create): 201 Created.
- PUT/PATCH (Update): 200 OK or 204 No Content.
- DELETE: 200 OK or 204 No Content.
- Validation Errors: 400 Bad Request or 422 Unprocessable Entity.
How to Set HTTP Status Codes in Code
Here is how to manually return a status code (like a 404 Not Found or 500 Server Error) in popular backend languages and frameworks:
http_response_code(404);
echo "Page not found";
exit;
res.status(404).send('Page not found');
return "Page not found", 404
Common Questions & Troubleshooting
1. What is the difference between a 404 and a 500 error?
It comes down to whose fault it is. A 404 Not Found means the server is working fine, but the specific page you want isn't there (like dialing a wrong phone number). A 500 Internal Server Error means the server itself crashed or has a bug (like the phone line being dead).
2. How do I fix a 502 Bad Gateway?
A 502 means one server (like a load balancer or Cloudflare) tried to talk to the main server, but got a bad response.
The Fix: If you are a user, refresh the page. If you are the website owner, check your firewall settings and make sure your backend service (like PHP-FPM) is running.
3. What is the difference between 301 and 302 redirects?
A 301 is permanent. It tells Google to forget the old URL and give all credit to the new one. A 302 is temporary. It tells Google "keep the old URL in your memory, we are just moving this for a short time."
4. Why am I getting a 403 Forbidden error?
This means the server hears you, but it refuses to let you in. This often happens if you try to view a sensitive file (like .htaccess), or if your IP address was blocked by a security plugin.
5. What is error 418 "I'm a teapot"?
This is a famous internet joke from 1998. It technically means "The server refuses to brew coffee because it is a teapot." You will almost never see this in real life unless a developer put it there as an Easter Egg.
6. Why do I see 429 "Too Many Requests"?
This is a rate limit. It means you (or a bot on your network) are hitting the server too fast. Just wait a minute and try again.
7. What are Cloudflare 520, 521, and 522 errors?
These are custom codes used by Cloudflare. They usually mean Cloudflare can't connect to your web host.
The Fix: Check if your web host is offline, or if your firewall is accidentally blocking Cloudflare's IP addresses.
8. What is a "Soft 404"?
This happens when a page says "Not Found" on the screen, but the server accidentally sends a "200 OK" code. This is bad for SEO because Google thinks it's a real page. You should always ensure your error pages send an actual 404 header.
9. How do I check the status code of a page?
You don't need a special tool. Just right-click anywhere on the page, select Inspect, go to the Network tab, and refresh the page. The first item in the list will show the status code (like 200 or 404).
10. What is the difference between 401 Unauthorized and 403 Forbidden?
A 401 Unauthorized means "I don't know who you are" (you need to log in). A 403 Forbidden means "I know who you are, but you aren't allowed in" (you lack admin or viewing permissions).
11. How do I find 404 errors on my website?
The easiest way is to use Google Search Console. Go to the "Pages" report and look for "Not found (404)". You can also check your server's access logs or run a broken link checker tool.
12. What is a 204 No Content response used for?
A 204 means the server did what you asked, but it has nothing to send back. This is very common in REST APIs when you delete an item, or when a webpage saves your progress in the background without needing to refresh the screen.