Codeigniter assets folder best practice

I'm fairly new to codeigniter, But i'm learning well, I'm going to add a css, images, js, ... folder but I'm not sure where to put it

Someone told me to make a 'public' folder

system
application
public
    css
    images

And then in your index.php ( in the public folder ) adjust accordingly

$system_path = '../system'; $application_path = '../application';

But when I do that i get a 404 ( not the ci 404, but a really-not-found one )

Anyone has any idea what I might be doing wrong? Thanks!


I have this setup:

application
assets
system
.htaccess

and in the "assets" folder i have subfolders like "img" or "js" and so on. I also use "utility helper" to help me point to that folder.

If you want to try it, you have to first create a helper named "utility_helper.php" with this code:

     <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

     if ( ! function_exists('asset_url()'))
     {
       function asset_url()
       {
          return base_url().'assets/';
       }
     }

and store it in

     application/helpers/

then you have to autoload that helper, you go to:

  application/config/autoload.php

and auto load the helper (example: )

  $autoload['helper'] = array('form', 'url', 'utility');

you have also to route to that folder ('application/config/routes.php')

       $route['assets/(:any)'] = 'assets/$1';

and have a .htaccess file with this content:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]

now you can simply include external scripts, css example:

   <link rel="stylesheet" type="text/css" href="<?php echo asset_url();?>css/style.css">

where css is the folder inside assets and style.css is the css file. Like so:

   application
   assets
          css
              style.css
   system

another angle on this -- and i think what they were trying to tell you to do - is putting your application & system folders one level up from the "public" html folder. that way your source files are not accessible. so like in Mamp the public folder is called htdocs, in most web hosting its called html.

 /application/html/
 /system     /html/

 // your main index page is in the public html folder
 /.…..       /html/index.php 

 // an assets folder in the public html folder with css, img, etc folders
 /.……        /html/assets/css/
 /.……        /html/assets/img/

then in your index.php, the path is going one level 'up' like

$system_path = '../system';
$application_folder = '../application';

BONUS - here are two tips that have helped me a lot. 1) Name your application folder, then you can easily switch between different versions, 'roll back' very quickly, etc.

 /app_alpha/html/
 /app_beta01/html/
 /app_beta02/html/

2) Put the base url on the index.php page -- not in the config.

   $assign_to_config['base_url'] = 'https://awesomedomain.com'; 

that way you can have a local development version, separate live server version -- and you never have to worry about over writing the base url because its on the index.php page - not in the config.


I would revert back the changes you made to your index.php, as it looks to me like they're the source of the problem.

I've been using a similar setup for CodeIgniter, whereby all my images, CSS, and JS go in a folder called static, and literally all I did was create that folder and put the files in it, and they worked fine. If you want to remove index.php from your URL's, you'll need to make sure that your .htaccess file doesn't rewrite the URL's for your static files.