# 404 Not Found in Flask

> Why Flask returns 404, how trailing slashes and blueprints affect it, and how to write a 404 handler that keeps the 404 status for HTML and JSON.

Canonical: https://scaplet.com/errors/http/404/flask

Guide Error · HTTP status codes

Why Flask returns 404, how trailing slashes and blueprints affect it, and how to write a 404 handler that keeps the 404 status for HTML and JSON.

3 min

read

3

sources

Oct 31, 2026

updated

Scaplet Editorial Team

author

404 4xx · Client error

The short answer

Flask returns 404 when no registered route matches the request, or when your code calls abort(404). Trailing slashes, converters and blueprint prefixes decide matching. A custom handler must return the 404 status itself, because Flask does not set it from the handler.

1. [Where a Flask 404 comes from](#where-a-flask-404-comes-from)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [A 404 handler that keeps the 404](#a-404-handler-that-keeps-the-404)
4. [What it means for SEO and crawlers](#what-it-means-for-seo-and-crawlers)

30-second check

## Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?

Summarize this page with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F404%2Fflask%20(%22404%20Not%20Found%20in%20Flask%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F404%2Fflask%20(%22404%20Not%20Found%20in%20Flask%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F404%2Fflask%20(%22404%20Not%20Found%20in%20Flask%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F404%2Fflask%20(%22404%20Not%20Found%20in%20Flask%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Sources of a Flask 404 and where to look

## Where a Flask 404 comes from

The Flask error-handling docs say a `NotFound` (404) error occurs if a request is sent to an unregistered route. That is the router, not your view code. The other source is your own code: `abort(404)` raises the same exception when a lookup finds nothing.

Source

### No rule matches the path

Who raises it

The router, before any view runs

Where to look

The route decorators and `url_map`; typos and prefixes

Source

### [Trailing slash](/glossary/trailing-slash) difference

Who raises it

The router

Where to look

Whether the rule ends in `/`

Source

### Converter does not match

Who raises it

The router: no rule matches

Where to look

`<int:id>` takes unsigned integers by default, so `/items/abc` matches no rule

Source

### Blueprint prefix

Who raises it

The router

Where to look

`url_prefix` on `register_blueprint`

Source

### Item not found

Who raises it

Your view, via `abort(404)`

Where to look

The lookup that returned `None`

On slashes, Werkzeug's routing docs call rules ending in a slash branches and rules without one leaves. With `strict_slashes` on, which is the default, requesting a branch without its trailing slash redirects to the slashed URL. The page does not document what a leaf rule does when requested with a slash, so test that case on your app instead of assuming.

_02_Fix it on your stack

## Reproduce and check it in 2 minutes

Public endpoint: a known 404
Public endpoint: a known 404

```
curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/404 # Expect: 404
```

Your own app: registered path, unregistered path, slash variant
Your own app: registered path, unregistered path, slash variant

```
curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:5000/about curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:5000/no-such-page curl -s -o /dev/null -w "%{http_code} %{redirect_url}\n" http://127.0.0.1:5000/about/ # Expect: 200, then 404, then either a redirect or 404 depending on how /about is declared
```

List what the router actually knows
List what the router actually knows

```
print(app.url_map) # every rule, its methods and its endpoint
```

Now your own Flask app. These need your dev server running (default port 5000; replace the paths with real and unregistered ones):

If the second request returns 200, a catch-all route or a handler returning the wrong status is answering for the missing page. If the first returns 404, the route is declared but under a different path or blueprint prefix, so print the rule list and compare it with the URL character by character.

_03_Check it

## A 404 handler that keeps the 404

Register a handler with `@app.errorhandler(404)`. The docs stress that the [status code](/glossary/http-status-codes) is not set from the handler's code, so return it explicitly. The classic mistake is returning a nice template with no status, which sends a 200 for a missing page:

For an API, raise with a description and return JSON from the handler. The docs' example reads the description out of the exception:

JSON 404 for API routes

```
from flask import abort, jsonify @app.errorhandler(404) def resource_not_found(e): return jsonify(error=str(e)), 404 @app.route("/cheese") def get_one_cheese(): resource = get_resource() if resource is None: abort(404, description="Resource not found") return jsonify(resource)
```

**Blueprint 404 handlers do not catch bad URLs**A 404 handler on a blueprint only runs when a view in that blueprint raises it or calls `abort`. A request to an invalid URL is a routing error before any blueprint is chosen, so register the handler on the app and branch on `request.path` if you want different pages per area.

A handler registered for `HTTPException` will also receive routing 404 and 405 errors, per the docs, so write it to preserve the status and name instead of flattening everything to one message.

HTML 404 page with the status set

```
from flask import render_template @app.errorhandler(404) def page_not_found(e): # note that we set the 404 status explicitly return render_template('404.html'), 404
```

_04_Explained

## What it means for SEO and crawlers

Google's documentation says it does not index URLs that return a 4xx status, and drops indexed ones that begin returning it. That makes a real 404 the right answer for a removed page. The failure to avoid is the custom page served with 200, which Google treats as a [soft 404](/seo-issues/soft-404-errors). Check with `curl -I` on a bad path and confirm the first line says 404. For the Django equivalent, see [404 in Django](/errors/http/404/django).

Sources · 3

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- palletsprojects.com
- google.com

1. 01 [Flask: Handling application errors](https://flask.palletsprojects.com/en/stable/errorhandling/) flask.palletsprojects.com · checked 2026-11-01
2. 02 [Werkzeug: URL routing (trailing slashes, converters)](https://werkzeug.palletsprojects.com/en/stable/routing/) werkzeug.palletsprojects.com · checked 2026-11-01
3. 03 [How HTTP status codes affect Google's crawlers](https://developers.google.com/search/docs/crawling-indexing/http-network-errors) developers.google.com · checked 2026-11-01

Questions

## Asked a lot.

2 questions people ask next, answered from the sources on this page.

_01_ Why does my custom Flask 404 page return status 200?

Flask does not set the status from the handler. Return the status explicitly, as in return render_template('404.html'), 404.

_02_ Why is my blueprint's 404 handler never called?

Blueprints cannot handle routing 404s, because the 404 occurs before a blueprint is determined. Register the handler on the app.

## Keep learning

[All errors](/errors)

[Learn 404 Not Found HTTP status codes](/errors/http/404) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next 404 Not Found in Django HTTP status codes](/errors/http/404/django) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 404 errors
[404 Not Found](/errors/http/404) [404 Not Found caused by .htaccess](/errors/http/404/htaccess) [404 Not Found in Django](/errors/http/404/django) [404 Not Found in IIS](/errors/http/404/iis) [404 Not Found in WordPress](/errors/http/404/wordpress)

More in 404 Not Found
[404 Not Found caused by .htaccess](/errors/http/404/htaccess) [404 Not Found in Django](/errors/http/404/django) [404 Not Found in Flask](/errors/http/404/flask) [404 Not Found in IIS](/errors/http/404/iis) [404 Not Found in WordPress](/errors/http/404/wordpress) [404 Not Found on Apache](/errors/http/404/apache) [404 Not Found on nginx](/errors/http/404/nginx)

Terms on this page
[Trailing slash in URLs](/glossary/trailing-slash) [HTTP status codes](/glossary/http-status-codes)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

Run the free audit, then approve the fixes. Every change can be undone.

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
