Welcome Guest, Not a member yet? Register   Sign In
Problem with base_url() and files not loading
#1

[eluser]copernicus[/eluser]
I am using base_url() to echo out my base url in my paths and it is working. I can see the correct path in the HTML and if I copy and paste it into the browser, it will go to the correct file. The problem is that the browser doesn't seem to be loading any of my CSS, JS, or images that are using base_url() to do the path.

For instance:
<img src="&lt;?php echo base_url(); ?&gt;assets/images/logo.jpg" alt="Reporter Ads" />

Will produce this HTML:
<img src="http://reportermag.com/beta/ads/adrequest/assets/images/logo.jpg" alt="Reporter Ads" />

The image file is there and works if you go to that URL but the browser doesn't seem to be loading it onto the page.
#2

[eluser]jedd[/eluser]
This is very likely a problem with your .htaccess file. You could post that, if the following doesn't work.

You need to add in an 'assets' exclusion, so that it doesn't get interpreted (and mis-directed). Mine looks like this:

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)

But the important thing is the 'assets' option, and note that you use vertical bars ( | ) to separate different directories at the root of your CI project.
#3

[eluser]Bryan Seeds[/eluser]
copernicus :

Yeah if your resources aren't loading then it is getting 'lost' in the rewrites.. edit your .htaccess like jedd had mentioned.. mine is like this :

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

And is your base_url() output right? your base = 'http://reportermag.com/beta/ads/adrequest/' ??

-Bryan
#4

[eluser]copernicus[/eluser]
Still doesn't seem to be working, here is my .htaccess file:

RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php?$1 [L]

The last line is because I am using QUERY_STRING

And yes, my base_url is "http://reportermag.com/beta/ads/adrequest/"
#5

[eluser]jedd[/eluser]
Does it work if you turn query_string off? (I've not played with it.)

Have you confirmed mod_rewrite is loaded okay in Apache ( do a phpinfo() page to confirm ).

Have you done an 'echo base_url()' for debugging purposes to make sure it's set to what you think it is set to.

Are you assuming that echoing the string 'uri/path/file.jpg' will somehow make a picture appear, or do you have this wrapped up in img src tags? Please post a few lines around the previously posted line of your web-page source (from your browser's point of view) again.

You may want to look at the img() function that comes with the the [url="http://ellislab.com/codeigniter/user-guide/helpers/html_helper.html"]HTML Helper[/url].
#6

[eluser]copernicus[/eluser]
mod_rewrite is ok, the problem seems to be with something that is using the 'src' attribute. For instance, loading a CSS file like this:
Code:
&lt;link type="text/css" rel="stylesheet" media="screen" href="&lt;?php echo base_url(); ?&gt;assets/css/reset.css" /&gt;

will work but trying to load a JS file or image file like this:
Code:
<img src="&lt;?php echo base_url(); ?&gt;assets/images/logo.jpg" />

But if I view source in the browser, I can copy those urls that are created, paste them in the browser, and view the files that way. They just aren't being loaded.

Some people mentioned earlier that it might be a .htaccess problem, here is my .htaccess:

Code:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
#7

[eluser]TheFuzzy0ne[/eluser]
First of all, this line is unnecessary:
Code:
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt)

The two lines below it ensure that the rewrite rule only fires if the directory/file exists. I think that this may be the line that's causing the problem, as all requests will always be passed to index.php, because the request URI never starts with index.php, images, assets, css, js or robots.txt - They always start with "/beta".

Good luck!
#8

[eluser]jamaky[/eluser]
I have similar problem with loading images. (application is on localhost and I use base_url() to create source path stored in database)
While css file load correctly, images don't. I don't understand it. Images don't load correctly on first load of page (including background). But after 5-10 refershes, image is loaded. After clear browser cache and delete temporary internet files the problem is back. I don't understand why browser can't load image of size 88kB on first time. I have any problem with images yet.

I tried to change .htaccess, config, and code. Without success. The source path is correct but image don't load (or load just partial).
Here's my .htaccess file
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css)
RewriteRule ^(.*)$ index.php/$1 [L]
config
Code:
$config['base_url'] = "http://localhost/cres/";
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
view script
Code:
&lt;link href=&lt;?php echo '"'.base_url().'css/main.css"';?&gt; rel="stylesheet" type="text/css"&gt;
...
// absolute address: $source = 'http://localhost/cres/images/general_figure/1.jpg'
<img src="&lt;?php echo $source;?&gt;" width="200" border="0">
...
// with base_url(), $source = 'images/coat_of_arms/1.png'
<img src="&lt;?php echo base_url().$source;?&gt;" width="200" border="0">
1. code generate
Code:
&lt;link href="http://localhost/cres/css/main.css" rel="stylesheet" type="text/css"&gt;
css file loaded
2. code generate
Code:
<img src="http://localhost/cres/images/general_figure/1.jpg" width="200" border="0">
image didn't load
3. code generate
Code:
<img src="http://localhost/cres/images/coat_of_arms/1.png" width="200" border="0">
Nothing dificult but.. image didn't load.
Both images are in these locations.
Please help.
#9

[eluser]TheFuzzy0ne[/eluser]
I'm at my wits end with htaccess files, so I will post mine (which works for me), and hope you can make it work for you.

Code:
AddOutputFilterByType DEFLATE text/css text/html application/x-javascript application/javascript

<IfModule mod_deflate.c>
        SetOutputFilter DEFLATE
        SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
        SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
        SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>

<IfModule mod_rewrite.c>
        RewriteEngine On

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

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

<IfModule !mod_rewrite.c>
        #If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        # Submitted by: ElliotHaughin

        ErrorDocument 404 /index.php
</IfModule>

It was stolen from the [url="http://codeigniter.com/wiki/mod_rewrite/"]CodeIgniter wiki[/url], and has had various tweaks added onto it over time.

Unfortunately, htaccess files seem to work differently from platform to platform. What works for me may not work for others.
#10

[eluser]jedd[/eluser]
What do your apache logs say?

Quote:2. code generate
Code:
<img src="http://localhost/cres/images/general_figure/1.jpg" width="200" border="0">
image didn't load

Can you definitely load http://localhost/cres/images/general_figure/1.jpg and http://localhost/cres/images/coat_of_arms/1.png from your browser?




Theme © iAndrew 2016 - Forum software by © MyBB