HTTP 404 Not Found — Causes, Fix & Custom Error Pages
The server cannot find the requested resource. The URL may be incorrect, the resource may have been deleted, or it may never have existed. 404 does not indicate whether the condition is permanent or temporary.
When to Return 404
Use 404 when a resource with the given identifier doesn't exist. For resources that existed but were permanently removed, consider 410 Gone. For protected resources where you don't want to reveal existence, return 404 instead of 403.
Common Causes
- Incorrect URL or typo in the path
- Resource deleted or never existed
- Outdated bookmark or link
- Wrong API version or endpoint path
- Case-sensitive URL mismatch
HTTP Response Example
HTTP/1.1 404 Not Found
Content-Type: application/json
{"error": "Not Found", "message": "User 123 does not exist"}Code Examples
Express.js
app.get('/users/:id', async (req, res) => {
const user = await db.users.findById(req.params.id)
if (!user) {
return res.status(404).json({
error: 'Not Found',
message: `User ${req.params.id} does not exist`,
})
}
res.json(user)
})
// Catch-all 404 handler (must be last)
app.use((req, res) => res.status(404).json({ error: 'Not Found' }))Next.js App Router
// app/users/[id]/page.tsx
import { notFound } from 'next/navigation'
export default async function UserPage({ params }) {
const { id } = await params
const user = await db.users.findById(id)
if (!user) notFound() // renders app/not-found.tsx with 404 status
return <UserProfile user={user} />
}Related Status Codes
Frequently Asked Questions
What does HTTP 404 Not Found mean?
The server cannot find the requested resource. The URL may be incorrect, the resource may have been deleted, or it may never have existed. 404 does not indicate whether the condition is permanent or temporary.
When should an API return 404?
Use 404 when a resource with the given identifier doesn't exist. For resources that existed but were permanently removed, consider 410 Gone. For protected resources where you don't want to reveal existence, return 404 instead of 403.
What causes an HTTP 404 error?
Common causes: Incorrect URL or typo in the path; Resource deleted or never existed; Outdated bookmark or link; Wrong API version or endpoint path; Case-sensitive URL mismatch.
