简体   繁体   中英

How to link to files and change base url in CodeIgniter?

Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css . It loads the one in the htdocs folder, but not the one in the css folder.

文件夹结构

Works:

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

Does NOT work:

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

I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.

My second problem is that every time I try to test a controller, I need to access it through index.php like this:

http://localhost:8888/index.php/test_controller

Is there a way to get rid of the need to put the index.php in the URL?

For your first problem:

Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:

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

NB: I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.

For your second problem: If you are using Apache place a .htaccess file in your root web directory containing the following:

RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

for better understand you can see codeigniter URL section through this link http://ellislab.com/codeigniter%20/user-guide/general/urls.html

and another one is http://snipplr.com/view/5966/codeigniter-htaccess/

For the second question: put the following in an .htaccess file inside your CodeIgniter root folder ( htdocs in your case):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]

Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php )

For the first question: try to make a folder called assets , or something similar and put it in the root of the site (again inside htdocs ). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css .

Also, in case you are using Linux box, make sure the folders you want to access have read permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM