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
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.
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.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
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.url_map; typos and prefixes/<int:id> takes unsigned integers by default, so /items/abc matches no ruleurl_prefix on register_blueprintabort(404)NoneOn 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.
curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/404
# Expect: 404curl -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 declaredprint(app.url_map) # every rule, its methods and its endpointNow 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.
@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:
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.
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'), 404Google'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.
2 questions people ask next, answered from the sources on this page.
Flask does not set the status from the handler. Return the status explicitly, as in return render_template('404.html'), 404.
Blueprints cannot handle routing 404s, because the 404 occurs before a blueprint is determined. Register the handler on the app.
Run the free audit, then approve the fixes. Every change can be undone.