How can I load css or js file in codeigniter 4?
In CodeIgniter 4, returning a 404 error when trying to load CSS or JavaScript files usually points to a fundamental misunderstanding of how the framework's public directory structure interacts with your server configuration. You are not alone; this is a common stumbling block for developers transitioning from monolithic frameworks like Laravel or older versions of CodeIgniter. The core issue lies in the distinction between the public/ folder and the document root defined by your web server.
The specific error you encountered, where link_tag('public/css/style.css') fails to load the asset, stems from how the URL helper constructs the path versus how your Apache or Nginx server resolves it. In CodeIgniter 4, the application directory defaults to public/. This means that a request to http://localhost/ is actually routing internally to index.php located in app/public/index.php.
However, your source material indicates a critical architectural change: you have moved your static assets (CSS and JS) directly into the htdocs root directory, outside of the CodeIgniter application structure. Your directory layout looks like this:
htdocs(Document Root)public/(CodeIgniter's public folder)css/style.css
app/(The application code)
When you use the link_tag helper or asset() functions, CodeIgniter generates a URL path relative to its own base path. If your CSS is physically located in htdocs/public/css/style.css, but your server treats htdocs as the root, the generated link often points to /public/css/style.css. While this might seem correct, many servers are configured where public/ inside the app folder is intended for web access, while files directly in htdocs sit outside the framework's security and routing logic.
To resolve the 404 error, you must ensure the file path matches exactly what the browser expects based on your server's document root. If your assets are in htdocs/public/css, the URL should indeed be /public/css/style.css. However, if you moved them there but they still load from the root, it suggests a mismatch between where the files physically reside and where the framework thinks they are.
The most robust solution within the CodeIgniter ecosystem is to keep assets inside the app/public/ directory or use the built-in asset management. If your public/ folder is located directly inside htdocs, you can reference it simply as:
<link_tag('css/style.css');
This tells the framework to look for the file relative to the application's public path. If that fails because your files are actually in htdocs/public, you must adjust the base URL configuration or move the files. A safer approach is to ensure your public/ folder inside the CodeIgniter project is the actual document root, or update your server configuration (like .htaccess) to serve requests from there correctly.
Another frequent pitfall involves the base_url() method combined with relative paths. If you are using link_tag('css/style.css'), CodeIgniter assumes the file is in app/public/css. If your files are in htdocs/css, the generated URL will look for http://localhost/public/css/style.css. If your server expects http://localhost/css/style.css, a 404 occurs.
To fix this without rewriting every link tag, you can configure the publicPath constant in your .env file or adjust the APP.PUBLICPATH setting to match your actual document root location relative to the project folder. However, the cleanest architectural pattern is to keep all assets within the CodeIgniter public/ directory and use link_tag() without specifying a leading slash or double slashes, letting the framework handle the path resolution automatically.
If you are using XAMPP, ensure that your Apache configuration points to the correct folder as DocumentRoot. If htdocs is the root, and your CI4 app lives inside it, you might need to symlink your asset folders or adjust the .htaccess rules to strip the index.php while preserving the public path for assets. Remember that CodeIgniter 4 does not automatically serve files outside its configured public directory unless explicitly told to do so via server configuration.
For production environments, relying on helper functions like link_tag is best practice because it abstracts away these path complexities. Always verify that your .htaccess file in the public/ folder correctly rewrites all requests to index.php, which is the entry point for CodeIgniter 4. This ensures that even if you reference a static file directly, the server knows how to route the request through the framework's security layers.
By aligning your physical file structure with CodeIgniter's expectationsâeither by moving assets into app/public or configuring the document root correctlyâyou can eliminate the 404 errors and ensure your stylesheets and scripts load reliably across different environments.