Showing your own 404 page in CodeIgniter 4
Page Not FoundShort answer
CodeIgniter 4 shows its built-in 404 view unless you register an override. Set $override404 in app/Config/Routing.php or call $routes->set404Override() in Routes.php. For a single missing record, throw PageNotFoundException in the controller. If you only want new wording, edit app/Views/errors/html/error_404.php.
Fix it fast
1Find out which cause you have
Open the route settings and see whether any override is registered. If nothing is set, the framework uses its default view.
grep -n "override404\|set404Override" app/Config/Routing.php app/Config/Routes.php2Match what you see, then jump to its fix
- 1
The grep above prints nothing, or only a commented lineNo 404 override is registeredFix - 2
Your page appears, but crawlers or tests see status 200The override returns HTTP 200 instead of 404Fix - 3
The route exists, but the controller finds no row and you show a blank page or an empty viewA missing record should end in 404 from inside a controllerFix - 4
Changes to the 404 text show in development but you see a generic message in productionYour edited error view is ignored in productionFix
- 1
What does the error look like?
Page Not FoundYour own 404 view or controller is never used; the framework's default page appears
Cause 1 of 4
No 404 override is registered
1Check
Without an override, CodeIgniter renders the view for the status code, app/Views/errors/html/error_404.php for web requests.
ls app/Views/errors/html/2Fix
Point the override at a controller method. It can be set as a property in app/Config/Routing.php or as a method call in app/Config/Routes.php; the call in Routes.php works the same way.
// app/Config/Routes.php
$routes->set404Override('App\Controllers\Errors::show404');
// app/Controllers/Errors.php
public function show404()
{
return view('my_errors/not_found');
}3Verify
Request an address that has no route and read the response status and body.
curl -i http://localhost:8080/no-such-pageConfirmed by codeigniter.com
Cause 2 of 4
The override returns HTTP 200 instead of 404
1Check
Before version 4.5.0 the 404 override answered with status 200. From 4.5.0 it defaults to 404.
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/no-such-page2Fix
Upgrade to 4.5.0 or later. On an older version, set the status code on the response inside your override method.
public function show404()
{
return $this->response->setStatusCode(404)->setBody(view('my_errors/not_found'));
}3Verify
The curl above now prints 404.
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/no-such-pageConfirmed by codeigniter.com
Cause 3 of 4
A missing record should end in 404 from inside a controller
1Check
Look for the place where the lookup returns null.
grep -rn "find(" app/Controllers | head2Fix
Throw PageNotFoundException. If an override is set, the override runs instead of the default page.
use CodeIgniter\Exceptions\PageNotFoundException;
$user = $model->find($id);
if ($user === null) {
throw PageNotFoundException::forPageNotFound();
}3Verify
Request an id that does not exist.
curl -i http://localhost:8080/users/999999Confirmed by codeigniter.com
Cause 4 of 4
Your edited error view is ignored in production
1Check
By default development and testing show a detailed report, while production shows a more generic message. Check the CI_ENVIRONMENT value in .env.
grep -n "CI_ENVIRONMENT" .env2Fix
Put your wording in app/Views/errors/html/error_404.php. When a view exists for the status code, it is shown in every environment, so keep error details out of it.
<!-- app/Views/errors/html/error_404.php -->
<h1>Page not found</h1>
<p>The address may be wrong, or the page has moved.</p>3Verify
Set CI_ENVIRONMENT = production and request a missing page.
curl -i http://localhost:8080/no-such-pageConfirmed by codeigniter.com
How do I stop it coming back?
- Keep one 404 view or one override in the project so every missing page ends the same way.
- Test a missing URL and a missing record after each upgrade, in production mode.
- Do not print exception messages or paths in the 404 view.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix