Codeigniter: Unable to access the Stylesheets

2019-02-19 00:23发布

问题:

I have the following directory structure of Codeigniter Folder. You can see that I have put all my assets in the assets folder at the root of the application.

Base URL is defined as

$config['base_url'] = 'http://kamran.dev/Codeigniter/application/';

I tried modify it to

$config['base_url'] = 'http://kamran.dev/Codeigniter/';

but that didn't work either.

Can anyone please have a look and tell me what I'm doing wrong here?

P.S. Then I removed the .htaccess file from the application folder that contains

Deny From All

And this worked for me. But that doesn't seem like a good idea.

回答1:

You have multiple options here. The easiest one is to move the assets folder to the same level as the application so that your directory structure is:

  • application
  • system
  • assets
    • css
    • img
    • js

Because the assets folder is not in a folder with the .htaccess with Deny From All it won't be blocked by your web server.

Alternatively you can add an .htaccess file in the assets folder with the following rule:

# /assets/.htaccess
Allow from all

I haven't tested this but I think it should work.

Another option is to change the .htaccess and put an exception on the assets folder but it's a very bad idea (with security implications) to remove the Deny From All without writing alternate rules to lock all folders that shouldn't be accessible.

Also, set your base_url to the folder containing application, system and assets directories (this folder contains Codeigniter's bootstrap/front controller index.php).

Additionally, I would advise you to echo your urls using the base_url() function:

base_url("assets/css/bootstrap.min.css");

OR to call a controller/route

base_url("controller/method");

The base_url function takes into account the value of $config['index_page'] as well as the necessary leading/trailing slashes.



回答2:

Change your base_url to the following:

echo base_url("assets/css/file-name-here.css");

You want to pass the URL string to the base_url function. This way, you can change $config['base_url'] to whatever you want and it will append the string to that URL properly.



回答3:

As an addition to the answer of grim. If you have your rewrite engine on you should have the following in your root .htaccess

<IfModule mod_rewrite.c>

RewriteEngine on

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

</IfModule>