How to load css in codeigniter

How to load css in codeigniter

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

Stefan Bogdanescu

Founder & Senior Architect

Resolving CSS Loading Errors in CodeIgniter 2

Encountering a 404 error when attempting to load an external CSS file in CodeIgniter is a common stumbling block, particularly for those transitioning from vanilla PHP frameworks or static sites. The root cause usually lies not in the CSS file itself, but in how the application's routing and URL rewriting are configured relative to how the browser requests resources.

When you place your all.css inside an application/css folder (or public/assets/css, depending on your structure) and attempt to link it using base_url(), the resulting URL often fails because of how CodeIgniter handles the index page requirement. In your specific scenario, you have configured $config['index_page'] = 'index.php'. This tells the framework that every valid entry point into the application must be routed through a file named index.php. Consequently, when you generate a link like http://webscarlets.com/ci/index.php/css/all.css, the server attempts to execute index.php as a script for that specific path. Since css is a directory containing static assets and not a controller action, PHP returns a 404 error because it interprets the request as an attempt to run a non-existent page rather than serve a static file.

To fix this in CodeIgniter 2.12, you have two primary architectural paths: disabling the index page requirement or ensuring the CSS resides within the public document root without triggering the controller logic.

Disabling the Index Page Requirement

The most robust solution for serving static assets like CSS and JavaScript is to remove the index.php requirement from your global configuration. By setting $config['index_page'] = '' in your application/config/config.php, you allow the server to serve requests directly to files within the public folder without rewriting them through the front controller.

However, this change requires a specific Apache or Nginx configuration to handle clean URLs correctly. If you are using Apache, ensure that .htaccess is located in your application/public directory and contains the standard rewrite rules:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /ci/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .^ index.php [L]
</IfModule>

Once index_page is cleared in the config and the rewrite rules are active, your link generation becomes straightforward. You can use the following in your view file:

<link rel="stylesheet" href="<?php echo base_url() . 'css/all.css'; ?>" type="text/css">

This generates http://webscarlets.com/ci/css/all.css. Because index_page is empty, the server treats /ci/css/all.css as a direct file path rather than an application route. This approach aligns with modern web standards where static assets are served directly from the web server's cache, bypassing PHP entirely for performance.

Verifying File Permissions and Location

If you prefer to keep index_page set to 'index.php', or if your hosting environment prevents modifying the .htaccess file, you must ensure the CSS folder is strictly within the public directory. In CodeIgniter 2.x, the standard structure places this at application/public. If your project root is different—for example, if public is actually the server's web root and your application sits inside a subfolder—you need to verify that base_url() correctly reflects the path excluding the script name when you intend to serve static files.

A frequent mistake involves placing the css folder directly inside the application folder (outside of public). If your web server root points to application/public, but your CSS is at application/css, the browser cannot access it because that directory is outside the document root, resulting in a 403 Forbidden or 404 Not Found error regardless of URL rewriting. Always double-check that static assets live within the folder served by your web server's DocumentRoot.

Migration Considerations

While resolving these issues in CodeIgniter 2 is straightforward, it is worth noting that newer frameworks like Laravel handle this automatically. In a modern Laravel application, static assets are placed in the public directory by default, and the framework does not require an index.php file to be appended to asset URLs unless explicitly configured otherwise. If you are planning a long-term migration from CodeIgniter 2 to a more modern stack, understanding these routing mechanics provides a smoother transition. Resources such as https://laravelcompany.com offer guides on how newer frameworks manage assets and routing differently, which might inspire optimizations for future projects.

By adjusting the index_page configuration or verifying your file structure relative to the public root, you eliminate the 404 error and ensure your stylesheets load efficiently across all browsers.

Our network