CodeIgniter htaccess and URL rewrite issues

CodeIgniter htaccess and URL rewrite issues

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

Stefan Bogdanescu

Founder & Senior Architect

Troubleshooting CodeIgniter HTAccess and URL Rewrite Errors

Removing index.php from your URLs is a standard requirement for modern web applications, allowing you to access resources via clean paths like localhost/ci/about. However, achieving this in CodeIgniter often trips developers up due to the interaction between Apache’s .htaccess configuration and the framework's routing logic. When you encounter a 500 Internal Server Error after placing an .htaccess file in your root directory, or when clean URLs simply fail to redirect requests to index.php, the issue usually lies in how the rewrite rules are scoped relative to your document root versus your application folder.

In the scenario described, the user correctly identified that the default .htaccess file provided by CodeIgniter in the application folder contains a restrictive Allow/Deny All directive, which effectively blocks external rewriting attempts within that subdirectory. The proposed solution involves creating a new .htaccess file in the root directory (e.g., localhost/ci/.htaccess) to intercept requests before they reach the application layer.

The core configuration relies on three directives. First, RewriteEngine On activates the module. Second, RewriteBase /ci sets the base URL for relative paths, ensuring that rewrite conditions are evaluated relative to the virtual host root rather than the physical filesystem path of the file itself. Third, the rules exclude existing files and directories (!-f, !-d) from rewriting, preventing access errors when loading assets like CSS or images. The final rule, RewriteRule ^(.*)$ index.php/$1 [L], captures any remaining request and appends it to index.php.

A critical pitfall in the provided example is the placement of this file relative to the project structure. If your root folder is localhost/ci and the application sits inside that folder, the .htaccess must be placed at localhost/ci/.htaccess, not inside the application subfolder. Placing it inside application works because Apache processes the directory from the root down; however, a rule in a subdirectory only affects requests targeting that specific subdirectory. To handle clean URLs for the entire application, the rewrite logic must reside at the highest level where the virtual host serves the content.

The 500 Internal Server Error often stems from a mismatch between RewriteBase and the actual URL structure, or from the server lacking mod_rewrite. While the user correctly commented that mod_rewrite.so is active in httpd.conf, enabling the module alone does not guarantee that the .htaccess file is being read if the server's security settings (like AllowOverride) are configured to ignore them. You must ensure that your Apache configuration explicitly allows overrides for the specific directory hosting your project.

Beyond the server-side configuration, CodeIgniter requires internal adjustments to handle the rewritten path. The framework defaults to expecting a file named index.php. If you remove this from the URL, you must set $config['index_page'] = '' in your environment configuration. Furthermore, relying on automatic detection via AUTO for $config['uri_protocol'] can be unreliable depending on how Apache passes variables. Explicitly setting $config['uri_protocol'] = 'REQUEST_URI' ensures that CodeIgniter reads the path from the HTTP request string as intended after the rewrite rule redirects it to index.php.

Finally, verify your file paths. The controller should reside at /application/controllers/about.php, and the view at /application/views/about_page.php. Ensure these directories are readable by the web server. If you face persistent issues where clean URLs return a 404 or 500 error despite correct configuration, consider creating an about directory in the application folder containing a default controller file; while not ideal for MVC purists, it can sometimes bypass routing quirks during development.

For those transitioning to newer PHP ecosystems, Laravel offers a more streamlined approach to this exact problem. The official documentation and community resources at https://laravelcompany.com often provide cleaner examples of .htaccess configurations that handle URL rewriting with fewer edge cases, though the underlying Apache mechanics remain consistent across frameworks.

Our network