Maaz Zindani

Loading the index…

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

CodeIgniter works locally but every page is 404 on the live server

Error404 Page Not Found

Short answer

A 404 on the live server comes from one of four places: the document root is not the public folder, Apache ignores .htaccess or nginx has no try_files rule, the route is not defined because auto routing is off, or the controller file name differs from its class in case. Test index.php directly to tell them apart.

  • Applies to CodeIgniter 4.5, PHP 8.3, Apache 2.4, nginx 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Request the front controller directly. If it works but nice URLs fail, the web server rewrite is the problem. If it also fails, look at the document root. If it renders a CodeIgniter 404, look at routes and controllers.

    curl -I https://example.com/index.php
    curl -I https://example.com/index.php/about
  2. 2Match what you see, then jump to its fix

    1. 1The web server 404s the URLs, while the app works when started from the public folderThe document root is the project folder, not publicFix
    2. 2index.php works, but any other URL gives the web server's own 404 pageApache ignores .htaccess, or mod_rewrite is offFix
    3. 3The nginx 404 page appears for every URL except /index.phpnginx has no rule that sends unknown URLs to index.phpFix
    4. 4CodeIgniter's own 404 page appears for a controller that exists and worked on an older setupThe route is not defined, because auto routing is offFix
    5. 5The controller file exists, but its URL gives a 404 and the file name and class name differ in caseThe controller file name does not match its classFix

What does the error look like?

  • 404 Page Not Found
  • The requested URL was not found on this server.

Cause 1 of 5

The document root is the project folder, not public

1Check

CodeIgniter 4 is designed to run from the public folder, which must be the document root. Check the root in the web server config.

grep -rn "DocumentRoot\|root " /etc/apache2/sites-enabled/ /etc/nginx/sites-enabled/

2Fix

Point DocumentRoot (Apache) or root (nginx) at the public folder of the project, and set AllowOverride All on it for Apache.

DocumentRoot /var/www/my-app/public
<Directory /var/www/my-app/public>
    AllowOverride All
</Directory>

3Verify

Test the config, reload, and request the home page.

sudo apache2ctl configtest && sudo systemctl reload apache2
curl -I https://example.com/

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 5

Apache ignores .htaccess, or mod_rewrite is off

1Check

The rewrite rules in public/.htaccess only apply when mod_rewrite is enabled and AllowOverride permits them.

apache2ctl -M | grep rewrite
ls -la /var/www/my-app/public/.htaccess

2Fix

Enable mod_rewrite, set AllowOverride All for the public folder, and reload Apache.

sudo a2enmod rewrite
sudo systemctl reload apache2

3Verify

Request a route that is not a real file.

curl -I https://example.com/about

Confirmed by codeigniter.com

Did this fix it?

Cause 3 of 5

nginx has no rule that sends unknown URLs to index.php

1Check

nginx does not read .htaccess. It needs try_files in the server block, and PHP files passed to PHP-FPM.

sudo nginx -T 2>/dev/null | grep -n "try_files\|root"

2Fix

Add the documented try_files rule inside the location for the site.

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

3Verify

Test the config, reload nginx and open a route.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by codeigniter.com

Did this fix it?

Cause 4 of 5

The route is not defined, because auto routing is off

1Check

Since 4.2.0, auto routing is disabled by default, so only routes defined in the routes file match.

grep -n "autoRoute" app/Config/Routing.php

2Fix

Define the route explicitly in app/Config/Routes.php.

// app/Config/Routes.php
$routes->get('about', 'Pages::about');

3Verify

Request the URL again.

curl -I https://example.com/about

Confirmed by codeigniter.com

Did this fix it?

Cause 5 of 5

The controller file name does not match its class

1Check

The controller file name must match the class name including case, with an uppercase first letter and the rest lowercase for auto routing. Compare the file with the class.

ls app/Controllers/
grep -n "^class" app/Controllers/*.php

2Fix

Rename the file to match the class exactly. If you want another naming style, define the route manually with the full class name.

$routes->get('helloworld', '\App\Controllers\HelloWorld::index');

3Verify

Request the URL again.

curl -I https://example.com/helloworld

Confirmed by codeigniter.com

Did this fix it?

How do I stop it coming back?

  • Make deploy scripts set the document root to public and keep the web server config in version control.
  • Test on the same OS and web server as production, or in a container that matches it.
  • Define every route in Routes.php and keep auto routing off.
  • Set baseURL in .env for each environment.

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

Next step

Want this handled for good?

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