4

This question has been asked several times, but I researched and still can't solve it. In one of my view file, I have (referencing to my css):

link rel="stylesheet" type="text/css"  href="/application/views/screen.css"

The css file is in:

- www
  - application
    - view
      - css
        - screen.css
  - system

I also tried to setup the css in the same folder under -www and use it directly by

link rel="stylesheet" type="text/css"  href="css/screen.css"

My base url is set to "" because I develop locally. Is this the issue? I'm Using wamp server.

So what is the problem? Can anyone help?

4 Answers 4

6

You can't put your CSS files or files to be served to browser in the application folder as it is protected for security reasons with a .htaccess file set to 'Deny from All' Your CSS, JS, image files etc, need to be outside of the application folder.

Put your CSS file in a folder called 'css' in the 'www' folder so it is NOT inside 'application' or 'system'. Then make sure you use the following (notice the leading slash, denoting an absolute URL):

href="/css/screen.css"

Use this instead:

# If your default controller is something other than
# "welcome" you should probably change this
RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ /index.php/$1 [L]
Sign up to request clarification or add additional context in comments.

10 Comments

link rel=​"stylesheet" type=​"text/​css" href=​"/​css/​screen.css", doesn't work. And this is what shows up in my Google Chrome using web tool
This is after you moved the files into a folder called 'css' inside the webroot folder 'www'?
yes,I have a folder of css under the www folder. and I inspect element and look at the http request header, this is what shows up : h t tp://localhost/css/screen.css
When you access localhost/css/screen.css do you get a 404, 403, 500, etc error?
re-enable it but comment out these lines # If your default controller is something other than # "welcome" you should probably change this RewriteCond $1 ^(index\.php|files) RewriteRule ^(.*)$ index.php?/$1 [L] RewriteRule ^(.*)$ /index.php?/$1 [L]
|
1

Bearing in mind there are several ways to skin a cat.

Quick fix without messing with htaccess. Use base_url() or site_url():

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

And use the following file structure:

  • www

    • application

      • views
      • (...)
    • system

    • assets
      • css
        • screen.css

4 Comments

@cryptic You're actually wrong. As long as the URL helper is loaded base_url() honors it's name and returns well...the base url. Thanks for the downvote anyway.
Agreed, I prefer this way of doing it, turns the URL into a permanent URL rather than a relative one too and from what I understand that's better SEO wise and saves headaches down the line.
link rel="stylesheet" type="text/css" href="localhost/assets/css/screen.css" this is what shows up in the browser. and I have the assets folder.
@AaronSantos If in assests folder I have a JS sub-folder, inside that if I have a file say script.js and I want to call base_url() on that file? Since it gets outside the application folder do you have any idea how to do that?
1

You should put all your asset files (images, js, css etc) into a seperate folder on the root of the site (outside of the application folder). Create a folder called 'files' and add a following line in your .htaccess file on the root of the site:

# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the files/ - js/, or css/ directories
RewriteCond $1 ^(index\.php|files)

This way you will have direct access to that files folder (and any file folder inside) through url.

5 Comments

I have a folder called css where application is stored and changed rewrite engine. When I typed in ht t p://localhost/assets, it doesn't work, it gives me a 404. By the way, my base_url = ' ' and I am developing locally, if that matters.
Ok, just to see if I understand you correctly, you have a folder called 'assets' in the root of you localhost, and a 'assets' rewrite condition in your .htaccess file and you still get the 404 error?
docs.google.com/document/d/… This is my rewrite engine. Still I can;t access assets folder with url. Under www folder I have -assets, -appliaction, and -system
Ok, then instead the RewriteCond $1 ^(index\.php|files) line you've added try this line RewriteCond $1 ^(index\.php|assets) and then try to access localhost/assets
still 404 and the url automatically changed form localho st/assets to h t t p ://localhost/assets/?/assets
1

I too faced the same problem in Windows. In Mac, it was easy thing. WHen trying to exclude index.php from URL, I faced 400 Bad Request Error . Later fixed this, by prefixing slash RewriteRule index.php, as below

RewriteRule ^(.*)$ index.php/$1 [L]

to

RewriteRule ^(.*)$ /index.php/$1 [L]

Going forward, faced another Issue: Could not load assets/static files. Also, i had my assets folder at root directory level. I saw a solution, like adding 'assets' in the RewriteCond to make it get excluded from applying rewriteRule. like,

RewriteCond $1 !^(index\.php|assets)       #It does not work for me

But, it does not work. I got 404 not found error. Later, i found the solution for this, as

RewriteCond $1 !^(index\.php|\/assets)     #Working!

And My Rewrite scripts are here:

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

Hope, this will help and saves plenty of time! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.