0

I am doing some testing with CI and have it running in a folder. http://www.example.com/sub-folder/ for instance

My directory structure is like this (where / is sub-folder/):
/
|-/system
|-/application

I have eclipse as my IDE with 2 projects, 1 for system, 1 for application, where application is my project that includes a reference to system.

My .htaccess file prevents access to system directory. I would like to have my images and CSS placed into: application/resources/images and application/resources/css respectively. I would like to be able to use <link rel="stylesheet" href="/sub-folder/css/style.css" /> However, after spending some time testing and searching, I haven't been able to get the CSS file to load from the application directory.

What do I need to change or add in my .htaccess to be able to do this?

The contents of my .htaccess follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

### Canonicalize codeigniter URLs

# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Mark Aroni
  • 605
  • 1
  • 10
  • 21

2 Answers2

2

Instead of

<link rel="stylesheet" href="/sub-folder/css/style.css" />

You could do

<link rel="stylesheet" href="<?php echo base_url('application/sub-folder/css/style.css'); ?>" />

I usually keep my assets in the root folder (where the main index.php file is) and use this asset library to load everything.

If you want to keep your assets in application/sub-folder you could do this:

Chris Schmitz
  • 8,097
  • 6
  • 31
  • 41
  • Thanks for your answer however If I put the assets into the root folder then they would be outside of my eclipse project which is what I wanted to avoid. I am using `base_url('sub-folder/css/style.css')` but thought I'd show `/sub-folder/css/style.css` for simplicity sake. – Mark Aroni Sep 30 '11 at 21:45
  • `base_url('sub-folder/css/style.css')` will look in the root of your application for the *sub-folder*. You would need to use `base_url('application/sub-folder/css/style.css')`. I updated my answer accordingly. – Chris Schmitz Sep 30 '11 at 21:57
  • I tried this and the HTML output is: `` is there something wrong with my configuration? – Mark Aroni Sep 30 '11 at 22:05
  • It flips *application* and *sub-folder* around? That's weird. What do you have your base_url set to? Did you try `site_url()` in place of `base_url()`? – Chris Schmitz Sep 30 '11 at 22:44
  • Sorry my mistake, the output is: `` when `base_url('application/resources/css/main.css')` is used. The .htaccess and index.php files are inside `sub-folder`. I have also tried `site_url()` with the same results. Also, `$config['base_url'] = 'http://www.example.com/sub-folder/';`. – Mark Aroni Sep 30 '11 at 22:59
  • 1
    Oh, I actually bet it's an issue with the RewriteBase you have set. Setting it to "/" will look in the server's root. You probably need to set it "/sub-folder". – Chris Schmitz Oct 01 '11 at 02:07
  • I adjusted the .htaccess like this: `RewriteBase /sub-folder` `RewriteRule ^css/([A-Za-z0-9-].+)$ application/resources/css/$1 [L]` however it only worked when I removed the .htaccess file from the application folder. Do I have to set my routes differently to allow this? I used the rewrite rule tester at http://martinmelin.se/rewrite-rule-tester/ to make sure that my rewrite rule was correct. – Mark Aroni Oct 01 '11 at 07:34
  • I am now using `base_url('css/main.css')` – Mark Aroni Oct 01 '11 at 07:40
0

I set up my CI projects like this:

  • application (my application files)
  • assets (publicly accessible css, js and images etc)
  • system (codeigniter core)

Typically, if I'm building in a CMS I'll put the CMS assets inside the application folder since they're basically private.

sitesbyjoe
  • 1,861
  • 15
  • 21