Images, CSS, JS disappear on any controller, but home page

#1
[eluser]Zelf[/eluser]
No doubt this is a newbie question....

I am developing on my localhost. I have the following .htaccess file:

RewriteEngine on
RewriteBase /fortlewis/
RewriteCond $1 !^(index\.php|images|global_assets|robots\.txt)
RewriteRule ^(.*)$ /fortlewis/index.php/$1 [L]

In config.php
$config['base_url'] = "http://localhost/mydirectory/";
$config['index_page'] = "";

When I access http://localhost/mydirectory/ all the images and css styling and javascript I have added to the view display no problems. When I go to http://localhost/mydirectory/othercontroller/othermethod the images, css, etc disappear. The view displays.

In page source on pages other than the default index page view I see a relative path to images, etc just like on the home page.

What bit of information I am overlooking here that I need to configure?

Also, If I turn off the .htaccess file and change config to:
$config['base_url'] = "http://localhost/mydirectory/";
$config['index_page'] = "index.php";

I still get the same results. So obviously it is not the .htaccess file causing this and I am overlooking some other factor here.

#2
[eluser]Zelf[/eluser]
Okay...

If I change the method name in the controller to index then the page loads just fine with images, but if the method name in the controller is e.g. "shipping" then none of the images display, but the view does display.

So http://localhost/mydirectory/information will display images (goes to function index in the controller by default)
But http://localhost/mydirectory/information/shipping will not

What am I missing here?

#3
[eluser]Zelf[/eluser]
It appears that I cannot use relative paths to images, css files, etc from other than the view loaded by the default controller or unless the controller calls the method index when the page loads. I don't get it.

e.g.

Code:
<link  href="http://localhost/mysite/global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />
will load on any page, but if I change it to:

Code:
<link  href="global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />
it will only load on the default controller index method or another controller's index method.

Anyone out there that can help me think through this?

#4
[eluser]intractve[/eluser]
if you use this (in your view):

Code:
<link  href="<?=base_url()?>global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />

your problem should be solved.

Relative URL's always load relative to whatever url you have in your browser's url box, so what happens when you load a different controller is that the browser looks for your css/image files at "http://localhost/mydirectory/information/shipping/global_assets/templates/default/css/body.css" which is not the right place it's stored in. by using the base_url() function you are outputting "http://localhost/mydirectory/" and then tacking on "global_assets/templates/default/css/body.css" which will work all over your site.

Have Fun

#5
[eluser]Zelf[/eluser]
[Solved]

[quote author="intractve" date="1283322761"]if you use this (in your view):

Code:
<link  href="<?=base_url()?>global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />

your problem should be solved.
[/quote]
Thanks. After some tinkering I figured this out. I used site_url(). However, I did not have the explanation of why it worked until now. Makes perfect sense now.

#6
[eluser]intractve[/eluser]
Glad it worked. :-)
Have Fun

#7
[eluser]pickupman[/eluser]
You don't even need to use that. Appending a http:// in front of the resources creates a http request the browser needs to resolve in order for the browser to determine where to download the file. Using base_url() can also be an issue if you need to use https://. You can easily use an absolute url without http://. Just use a forward leading /.
Code:
<link  href="/global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />

The leading slash means the path is always relative the root folder of the site. Without the leading slash the path is relative to the uri. Since CI uses SEO friendly URI's the browser interprets them like folders.

#8
[eluser]Zelf[/eluser]
[quote author="pickupman" date="1283328000"]
Code:
<link  href="/global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />
[/quote]
This did not work for me although I would much rather use "/" instead of the site_url() or base_url() to deal with http and https.

Is there a setting in mod_rewrite or in apache so I can use a "/" and absolute paths?

#9
[eluser]pickupman[/eluser]
This should do it for you
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

This checks if a real file or directory exists, and routes everything else to index.php

#10
[eluser]cahva[/eluser]
Pickupman's example didnt work as the url was wrong if this is used from your localhost where the site starts from "mysite". So the correct would have been:
Code:
<link  href="/mysite/global_assets/templates/default/css/body.css" rel="stylesheet" type="text/css" />

..but ofcourse that will get you in trouble when you move your site to production server(without "mysite"). To get the goodies from both worlds, use base_url(or site_url) in your views and define your base_url in the config.php:
Code:
$config['base_url']    = "/mysite/";

And when in production server you can shorten that to "/". To make it automatic, you can do if else to check what server you are on(using $_SERVER['SERVER_NAME']) and define the base_url according to that.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.