I think the above answer is good, but since CI is dynamically writing URLs, I prefer this method. Also, I think this helps with writing better template code. (I tested this specifically on MAMP.)
For one, set the .htaccess file to the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Create a new helper in application/helpers
. I call mine assets_helper.php
but call it whatever is useful. Put this code in that helper file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('asset_url()')) {
function asset_url() {
echo base_url().'assets/';
}
}
Add the helper to your autoload file in config/autoload.php
a la
$autoload['helper'] = array('url', 'assets');
(You may not already have the url
helper activated, but it's common.)
And also add a route for the assets folder (in config/routes.php
):
$route['assets/(:any)'] = 'assets/$1';
Now when you want to add css, or js or images, you just need to put <? assets_url(); ?>
in the template code.
<img src="<? asset_url(); ?>images/logo.png" width="100" height="100" />
or
<link rel="stylesheet" href="<? asset_url(); ?>css/house.css">