Welcome Guest, Not a member yet? Register   Sign In
sub-directory images and css problem


Sorry if this is a repeat of a question. If it is please redirect me to the solution as I have navigated around the forum trying to find the solution with no success. I have also done the CI tutorials in the User Guide which is bundled with CI, but this has not helped.

My setup is as follows: the website developed with CI is located in two sub directories down on the host server - i.e. CI's index.php is located at http://domainname/subdir1/subdir2/index.php

I have my .htaccess file setup according to the CI Wiki at http://codeigniter.com/wiki/mod_rewrite/.
My resulting .htaccess looks like this:
RewriteEngine On
RewriteBase /subdir1/subdir2/

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

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

and I have set
$config['index_page'] = '';
in system/application/config/config.php and have all the correct mod_rewrite settings set according to the wiki.

The default controller is called defaultcontroller and there is a function in this controller called index which calls a view called aview.

The aview view has HTML code in it to link to the stylesheet that looks like this:
<link rel="stylesheet" type=text/css" href="stylesheet.css" />
and an example image in the view is used like this:
<img src="images/image1.jpg" title="image title" alt="image title" />

My directory structure as you can guess is

If go to to http://domainname/subdir1/subdir2/ everything works fine and all images and the css styleshheet used by the view are loaded correctly.

However, if I go to http://domainname/subdir1/subdir2/defaultcontroller/ or http://domainname/subdir1/subdir2/defaul...ler/index/ the website generated by the HTML is shown but all the images and CSS layout are no longer shown (i.e the links to the CSS and images are now broken)

In Firefox the properties for the place holders for the now broken image show that the image is resolving to http://domainame/subdir1/subdir2/default...image1.jpg which obviously doesn't exist.
Just to be clear, the correct image link (and the link generate when I go to just http://domainname/subdir/subdir2) should be http://domainame/subdir1/subdir2/images/image1.jpg

How do I fix this (either in the .htaccess or somewhere in the PHP code, so that the view always links to the correct image irrespective of whether I include controllers and functions in the URI?

i think all you have to do is prepend your css and image paths with a /.

so href="/stylesheet.css" and src="/images/image1.jpg"

try that and see if it works.

you may need to add the 'images' folder to the .htaccess file as a 'donotrewrite' style rule, otherwise it will end up as: http://www.example.com/index.php/images/image1.jpg

thanks elvix and ray73864, but the solutions didn't really help, because the codeigniter powered website is located to directories down from the base domain name i.e. the system folder in located in http://domainname/subdir1/subdir2/
So adding in a slash, as suggested by elvix, just looks for images in http://domainname/images/ which is incorrect.

Anyway, I think the way to solved it is to use the HTML Helper.

Theme © iAndrew 2016 - Forum software by © MyBB