The requested URL was not found on this server

The requested URL was not found on this server

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing Apache 404 Errors in CodeIgniter: Beyond the Obvious

When a CodeIgniter application deployed on an Apache server renders the homepage correctly but returns a "Not Found" error for subsequent routes, the issue almost never lies within the PHP logic itself. The error message The requested URL /site/ourservices was not found specifically indicates that Apache is rejecting the request before it even reaches the framework's routing layer. This usually stems from a misconfiguration of the .htaccess file or an incorrect document root setting in your virtual host configuration.

CodeIgniter relies heavily on URL rewriting to transform human-readable URLs like /site/ourservices into internal paths that the controller can understand, typically something like index.php/site/ourservices. Apache handles this transformation using the mod_rewrite module. If the rewrite rules are missing, malformed, or placed incorrectly, Apache will attempt to serve a file literally named /site/ourservices, fail to find it in your directory structure, and return a 404 error.

The most common culprit is an absent or incomplete .htaccess file in your project's root directory. This file must contain the specific rewrite rules that strip the index.php from the URL path while preserving the rest of the query string. A standard, robust configuration looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /site/
    
    # If a file or directory doesn't exist, rewrite to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Redirect all other requests to index.php, preserving the path
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

# Ensure directory listing is disabled for security
Options -Indexes

Notice the RewriteBase /site/ directive. This is critical if your project is hosted in a subdirectory. If your virtual host points to /var/www/site, that base must match exactly. Without this, Apache might attempt to rewrite requests relative to the wrong root, causing valid routes to be treated as non-existent files.

Another frequent pitfall involves the ErrorDocument directive mentioned in your error log. The message states: Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. This implies that you have configured Apache to display a custom 404 page (perhaps pointing back to index.php), but the rewrite rule intended to catch the request and serve that custom page is failing.

When debugging, check the order of operations. Apache processes directives sequentially. If you have a RewriteRule before your ErrorDocument, and the rule fails to match or redirect, Apache falls back to the default error handler. However, if the mod_rewrite module isn't enabled or the AllowOverride directive in your virtual host is set to None or Deny, the .htaccess file is effectively ignored entirely. In that scenario, you must move the rewrite rules into the main server configuration file (e.g., /etc/apache2/sites-available/your-site.conf) rather than keeping them in .htaccess.

Consider the trade-off between using .htaccess and virtual host configurations. .htaccess files offer flexibility for small projects but incur a performance penalty because Apache parses them on every request. For production environments handling high traffic, consolidating these rules into the server block is a best practice to reduce overhead.

If you have recently updated Apache or moved your project files, ensure that file permissions haven't changed. While .htaccess files do not typically require execute permissions, they must be readable by the Apache user (usually www-data or apache). A permission error here can silently cause rules to fail depending on the server setup.

Finally, verify that mod_rewrite is actually loaded. You can check this by running a command like apachectl -M | grep rewrite. If it's missing, Apache cannot perform any URL rewriting, rendering your CodeIgniter routing useless until you enable the module via a2enmod rewrite and restarting the service.

Sometimes, the solution is as simple as ensuring the document root in your virtual host configuration points exactly to the directory containing the public folder or the application root where .htaccess resides, rather than a parent directory that doesn't contain these files. Once these structural elements align, Apache will successfully rewrite the incoming request, pass it to index.php, and CodeIgniter can render your views as intended.

Our network