Free auditStart free
GuideError · HTTP status codes

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.

3 min
read
3
sources
Oct 31, 2026
updated
Scaplet Editorial Team
author
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
  2. Reproduce and check it in 2 minutes
  3. A 404 handler that keeps the 404
  4. 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 withChatGPTClaudePerplexityGoogleAI Mode
02Fix it on your stack

Reproduce and check it in 2 minutes

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
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
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.

03Check it

A 404 handler that keeps the 404

Register a handler with @app.errorhandler(404). The docs stress that the status code 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)

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
04Explained

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. Check with curl -I on a bad path and confirm the first line says 404. For the Django equivalent, see 404 in 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. 01Flask: Handling application errorsflask.palletsprojects.com · checked 2026-11-01
  2. 02Werkzeug: URL routing (trailing slashes, converters)werkzeug.palletsprojects.com · checked 2026-11-01
  3. 03How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Questions

Asked a lot.

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

01Why 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.

02Why 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.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

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

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane