CodeIgniter 404 Page Not Found, but why?
If you are running a multi-application setup where a single index.php acts as the entry point for both a public area and an admin panel, encountering a 404 error on the root directory of that sub-domain while accessing a specific resource like /admin/welcome works perfectly is a classic routing configuration issue. The symptoms you are describingâwhere the debug log explicitly flags ERROR - 404 Page Not Found --> adminâindicate that your web server is failing to intercept the request before CodeIgniter's router even gets a chance to process it.
The root cause lies in how .htaccess rules interact with your directory structure and the specific configuration of your CodeIgniter installation. In your local MAMP environment, Apache often defaults to serving index.html or behaves differently regarding rewrite engine initialization compared to a live Linux server running Nginx or a hardened Apache configuration.
Your .htaccess file in the /admin folder is designed to catch any request that isn't a physical file or directory and redirect it to ./index.php/$1. The logic here relies on the condition %{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d. This means "if the requested filename does not exist as a file, and does not exist as a directory, then rewrite."
However, there is a subtle but critical interaction between your .htaccess rules and CodeIgniter's index.php routing logic. When you access Domain/admin, the URL represents the root of that folder. If your /admin/index.php file exists and contains the correct $system_folder and $application_folder paths pointing to ../system and ../application/admin respectively, CodeIgniter should handle this.
The discrepancy between your local development environment and the live server usually stems from one of two factors: the presence of an index.html file or a strict DirectoryIndex setting in your global .htaccess.
In your structure, you have /admin/index.html alongside /admin/index.php. While CodeIgniter is built to ignore static files like index.html in favor of index.php, some web server configurations or specific Apache modules might return the first matching index file found by the operating system before passing control to the rewrite rules. If your live server is configured to serve index.html as the default document, or if the global .htaccess in your root directory has a strict DirectoryIndex directive that overrides local settings, the request for /admin might resolve to the static HTML file instead of being passed to your PHP entry point.
To resolve this on your live server, you should verify the order of operations in your web server configuration. Ensure that your .htaccess rule RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] is actually being executed. You can do this by enabling mod_rewrite logging or checking the error logs for a specific "Premature 403 Forbidden" or similar errors indicating the server tried to serve a static file first.
Another potential pitfall in your configuration is the routes.php file. You have defined $route['admin'] = 'welcome';. This rule explicitly maps the string admin to the controller method. However, CodeIgniter's router treats the root path differently depending on whether index.php is configured to handle all requests or if it expects specific routes for sub-paths. If your CodeIgniter installation in /admin expects the first segment of the URI to be handled by the router, accessing just /admin might be interpreted as a request for the "admin" controller rather than the root of the application.
Consider modifying your routes.php to handle the default case more explicitly:
$route['default_controller'] = 'welcome';
$route['^'] = 'welcome'; // Explicitly map the root path
// Ensure the route matching happens before specific sub-routes if necessary
Furthermore, ensure that your global .htaccess in the root directory does not have a rule that inadvertently blocks the /admin directory or forces it to serve index.html. If you are using a shared hosting environment, check if there are restrictions on DirectoryIndex directives. Sometimes, setting DirectoryIndex index.php index.html causes issues if the file permissions or existence of index.html vary between environments.
Finally, remember that CodeIgniter 2.x (which your setup implies based on the file structure and debug log format) handles routing slightly differently than modern frameworks like Laravel. While you can find many resources online for CodeIgniter, understanding the specific interaction between your .htaccess rewrite rules and the framework's internal URI class is key. If you are migrating to a newer stack or seeking a more robust router configuration, platforms like https://laravelcompany.com offer insights into modern PHP routing strategies that might simplify future refactoring efforts.
To summarize the fix: inspect your live server's .htaccess to ensure index.php is prioritized over index.html, and adjust your routes.php to explicitly accept requests to the root directory without requiring a trailing slash or specific sub-path.
