strange behaviour in codeigniter with loading of css, js, img fileswhen slash (/) added at end of url
im quite aware of Codeigniter but came across this problem while trying to convert a static site into CI. The problem happens while loading css files when certain urls are used. Im trying to strictly keep php away from views (infact want to have only .html pages for views).
I have also searched many forums for a solution for loading css. I have come across some solutions but they help only if you are making a completely new site in CI. I am aware of
using before the href link to make it site independant
but do not want to use this, cause i want to convert an existing static website into CI. which would mean changing each and every href at every file. (seems like a nightmare if there are many pages).
I am having trouble loading the css and other resource files in certain conditions. Below is a description of what all i have done so far in the project.
- used .htaccess at root to remove "index.php" file
- used standard folder structure that comes with codeigniter 2.0.2
- all images and resources (Eg. css, img, js . . . etc) are placed directly in the root folder (i.e. where the application & system folders are located)
- all resources are accessed from the view files as if the html files were in the root folder along w开发者_如何学Pythonith the resources (Eg. href="css/style.css")
Below is a sample description of the controller
class Blog extends CI_Controller {
public function index(){
$this->load->view('blog.html');
}
public function posts(){
$this->load->view('post.html');
}
}
Now the problem i am facing a problem is when i use certain url for the "index" function . . the css gets loaded correctly.
But if i add a slash after that or use any of the other functions. there is a problem loading the css.
Eg.
/* This url loads css correctly */
http://localhost/ci-test/blog
/* these urls do not load css */
http://localhost/ci-test/blog/
http://localhost/ci-test/blog/posts
Is there something i am doing wrong? or some problem with my .htaccess file ? (included below)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
What do i need to do to import the current website into CI with minimal effort.
You might try something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
Not sure if that will last rule will work because i dont know how CI's routing works, but its what im used to with Zend and Symfony routers.
all resources are accessed from the view files as if the html files were in the root folder along with the resources (Eg. href="css/style.css")
Well, that can't work if the external URL the browser sees may have many levels of nested directories, can it? To make a relative URL work from ci-test/blog
you would have to reference css/style.css
, whereas to make it work from ci-test/blog/posts
it could only be ../css/style.css
.
I suggest replacing all the references with rooted URLs like /ci-test/css/style.css
, or if that's too much work using a <base href="/ci-test/">
or similar in the <head>
to make all relative URLs behave as related to a rooted URL.
精彩评论