CodeIgniter Forums
sub-directory images and css problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: sub-directory images and css problem (/showthread.php?tid=12425)



sub-directory images and css problem - El Forum - 10-18-2008

[eluser]doehoe[/eluser]
Hi

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:
Code:
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
Code:
$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:
Code:
<link rel="stylesheet" type=text/css" href="stylesheet.css" />
and an example image in the view is used like this:
Code:
<img src="images/image1.jpg" title="image title" alt="image title" />

My directory structure as you can guess is
Code:
-index.php
-stylesheet.css
+system
+images
  image1.jpg
  image2.jpg

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/defaultcontroller/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/defaultcontroller/images/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?


sub-directory images and css problem - El Forum - 10-18-2008

[eluser]elvix[/eluser]
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.


sub-directory images and css problem - El Forum - 10-19-2008

[eluser]ray73864[/eluser]
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


sub-directory images and css problem - El Forum - 10-19-2008

[eluser]doehoe[/eluser]
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.