Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Showing your own 404 page in CodeIgniter 4

ErrorPage Not Found

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

  • Applies to CodeIgniter 4.5, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1The grep above prints nothing, or only a commented lineNo 404 override is registeredFix
    2. 2Your page appears, but crawlers or tests see status 200The override returns HTTP 200 instead of 404Fix
    3. 3The 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. 4Changes to the 404 text show in development but you see a generic message in productionYour edited error view is ignored in productionFix

What does the error look like?

  • Page Not Found
  • Your 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-page

Confirmed by codeigniter.com

Did this fix it?

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

2Fix

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

Confirmed by codeigniter.com

Did this fix it?

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

2Fix

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/999999

Confirmed by codeigniter.com

Did this fix it?

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

2Fix

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

Confirmed by codeigniter.com

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.