Why Removing index.php Often Fails in CodeIgniter
You have set $config['index_page'] to an empty string and configured your .htaccess file with standard rewrite rules, yet the URL still loads as mysite/index.php/slug. This is a classic configuration bottleneck where two specific conditions prevent clean URLs from functioning: the absence of a virtual host definition for your project in Apache's main configuration, and a potential mismatch between your server's document root and your application's physical location.
When you set $config['index_page'] = '', you are telling the CodeIgniter framework to generate links without the index.php query string parameter. However, this alone does not strip the file from the actual URL requested by the browser. The critical factor is how Apache handles requests that do not match a physical file or directory on the disk.
The Role of mod_rewrite and Virtual Hosts
The .htaccess snippet you provided relies entirely on mod_rewrite. For this to work effectively in a production environment, Apache must process the rewrite rules before trying to serve static files like images or CSS. If your main domain is hosted via a virtual host definition in /etc/apache2/sites-available/, the server context might differ from what your local .htaccess expects.
Specifically, if your project is located at /var/www/myproject but your Apache Virtual Host points to /var/www/html, or if the Directory block inside the vhost configuration does not explicitly enable RewriteEngine and allow overrides, the .htaccess file will be ignored entirely. In many modern setups, especially with PHP-FPM or specific hosting providers, the DirectoryIndex directive in the main Apache config might be overriding your framework's setting.
You need to ensure that the rewrite rules are applied to the directory containing your CodeIgniter installation. If you are using a generic setup where the public folder is distinct from the web root (e.g., /var/www/myproject/public), Apache will often serve the index.php file directly if it exists in the requested path, bypassing the rewrite logic entirely because the file is found before the rule triggers.
Correcting the .htaccess Logic
The most common reason for this failure is that the server is successfully finding a physical file or directory that matches the URL, causing mod_rewrite to skip the rule. The standard CodeIgniter .htaccess configuration ensures that if a file does not exist on the disk, Apache attempts to rewrite the request to index.php.
Your current rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
This logic is sound. The issue usually lies in the order of operations or the presence of a DirectoryIndex directive elsewhere in your Apache configuration that conflicts with CodeIgniter's internal routing.
To ensure clean URLs work, verify that no other directory block is setting a custom DirectoryIndex. Additionally, ensure that the .htaccess file is placed in the root folder of your application (the folder containing index.php), not in a parent web root where static assets reside. If you are deploying via Git or a package manager, ensure that the .htaccess file is included in the deployable directory structure and has the correct permissions (644).
Troubleshooting Steps
If you have verified the file locations and rules but still see index.php in the address bar, check your Apache error logs (/var/log/apache2/error.log) for "Permission denied" or "Cannot find .htaccess" messages. Sometimes, PHP's open_basedir restrictions or SELinux policies can block .htaccess from being read even if permissions look correct.
Another subtle pitfall involves the URL rewriting module itself. While you mentioned mod_rewrite is enabled, ensure that RewriteBase is set correctly if your application is not in the root of the domain. If your site is at mysite.com/project, your .htaccess should ideally be:
RewriteBase /project/
Without this, relative paths might resolve incorrectly, leading to 404 errors or unexpected behavior where the framework thinks it failed to route and defaults to showing the index page with parameters.
Modern Alternatives
If configuring Apache proves difficult due to server restrictions or complex virtual host setups, consider using Nginx, which handles URL rewriting differently and is often more straightforward for this specific use case. Alternatively, if you are moving to a newer PHP version or framework ecosystem, tools like Laravel offer robust routing configurations that abstract away much of the low-level Apache configuration. For instance, similar clean URL strategies are discussed in detail by experts at https://laravelcompany.com, where they emphasize the importance of checking server configuration over application settings first.
By aligning your Apache virtual host configuration to explicitly allow overrides and ensuring your .htaccess file is in the correct directory, you can achieve the clean URL structure mysite/slug without the framework's entry point interfering with the request flow.
