EllisLab text mark
Advanced Search
1 of 2
1
   
Second display gives 404 error on style sheet
Posted: 08 March 2012 01:08 PM   [ Ignore ]
Joined: 2011-02-09
40 posts

I’m trying to do a simple site with an application wide template view:
The view template:

<!DOCTYPE html>
<
html>
<
head>
<
title<?php echo $title?></title>
<
link rel='stylesheet' href='assets/styles/site_style.css' />
</
head>
<
body>
<
div id="wrapper">
<
div id="header">
<?php echo $page_title?><br/><br/>
<
a href="home/about">About Us</a>
</
div>
<
div id="content">

<?php echo $content?>
</div>
<
div id="footer">
  
Copyright &copy<?php echo date('Y')?> by Client NameAll rights reserved.
</
div>
</
div>
</
body>
</
html

The controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Home extends CI_Controller {

  
 
public function index()
 
{
  $this
->showpage('Home Page''Home Page''home');
 
}
  
  
public function about()
  
{
  $this
->showpage('About Us''About Us''about');
 
}

  
public function showpage($title,$page_title,$template)
  
{
  $data[
'title'$title;
    
$data['page_title'$page_title;
    
$data['content'$this->load->view($template,'',TRUE);
    
$this->load->view('app_template',$data);
  
}
    
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */ 

The index display works fine.

When I call for the about page it has no css and Firebug shows a 404 under the link tag.


Any thoughts?

Ed

 

 
Posted: 08 March 2012 01:20 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

Don’t use a relative path for your stylesheet, or anything else.

 Signature 
 
Posted: 08 March 2012 01:33 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-02-09
40 posts

Thanks, I’ll try it.
Still don’t understand why it would work from the index function…

 
Posted: 08 March 2012 05:29 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Because when you use relative URLs, it adds the link to the stylesheet to the end of whatever URL you’re currently looking at.

So your homepage would look for a CSS file here: example.com/assets/styles/site_style.css (which is typically accurate).

If you were on a different level folder, it would look for it there: example.com/anotherpage/assets/styles/site_style.css (not accurate, 404).

 
Posted: 08 March 2012 07:21 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts
<!DOCTYPE html>
<
html>
<
head>
<
title<?php echo $title?></title>

<!-- 
like this -->
<
base href="<?php echo base_url();?>" />
<
link rel='stylesheet' href='assets/styles/site_style.css' />

<!-- or 
you can do it like this -->
<
link rel='stylesheet' href='<?php echo base_url();?>assets/styles/site_style.css' />

</
head>
<
body>
<
div id="wrapper">
<
div id="header">
<?php echo $page_title?><br/><br/>
<
a href="home/about">About Us</a>
</
div>
<
div id="content">

<?php echo $content?>
</div>
<
div id="footer">
  
Copyright &copy<?php echo date('Y')?> by Client NameAll rights reserved.
</
div>
</
div>
</
body>
</
html

 

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 08 March 2012 08:22 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-02-09
40 posts

Thanks for the help guys.

The base_url addition made everything better…

 
Posted: 18 April 2012 06:18 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2010-03-01
72 posts

Honestly, as a best practice I would recommend that everyone go with the latter version you presented (below). The primary reason is if you ever enable SSL for your website, you won’t get a bunch of 404 errors for your asset files; this avoids having to recode your template layout and changing the src/href for each asset file (image, css, js etc). I’m doing that now.

<link rel='stylesheet' href='<?php echo base_url();?>assets/styles/site_style.css' /> 

——-see below, this won’t work——for SSL you must use relative path

 
Posted: 18 April 2012 06:38 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2010-03-01
72 posts

I stand corrected! The previous won’t work, because of SSL, you need to use relative path, and my home page relative would be different from my interior files, which will cause an error.

Thoughts anyone on a work around?

 
Posted: 18 April 2012 06:40 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts
<link rel="stylesheet" href="<?php echo base_url('assets/styles/site_style.css');?>" /> 

 

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 18 April 2012 06:41 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts
<link rel='stylesheet' href='/assets/styles/site_style.css' /> 

Should work for either case.  Just make sure to start the path with a / so it knows to start at the root of your site instead of relative to the current request.

 Signature 
 
Posted: 18 April 2012 06:47 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts
InsiteFX - 18 April 2012 06:40 PM
<link rel="stylesheet" href="<?php echo base_url('assets/styles/site_style.css');?>" /> 

The reason why I don’t like that is you can’t then just use base_url() for BOTH SSL and non SSL links/assets (without changing config before use).  Like if you only have SSL enabled on your “checkout” page but the rest are normal.  Just starting the url with “/” will work in both cases.

 Signature 
 
Posted: 18 April 2012 06:51 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

Good point CroNix, I have not had to use SSL yet but thanks for the poniter…
smile

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 18 April 2012 06:56 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-03-01
72 posts

@CroNix, yours look plausible, but it doesn’t work for me in localhost.. but maybe it will work in production?

So I would access my local website as http://localhost/website/  with the below for my htaccess file

RewriteEngine on
RewriteCond 
$!^(index\.php|images|assets|js|stylesheets|scripts|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /website/index.php/$1 [L] 
 
Posted: 18 April 2012 07:05 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

It’s because you have your dev site in a subdirectory (and so document root is wrong as its not pointing to your subdirectory).  It’s quite helpful to set up virtual hosts in apache, one for each project, and set the document_root to that directory so you don’t have to worry about a subdirectory and your dev site will work just like the live site would.  You would access it by http://yoursite.localhost instead of http://localhost/website.

Personally, I like to have my production env and dev env the same and avoid inconsistencies like this.

 Signature 
 
Posted: 18 April 2012 09:44 PM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2010-03-01
72 posts

Sorry for the digression in topic, but others who will try to enable SSL may find the below helpful.

@CroNiX. Could you share with me how you setup your .htaccess? I was able to setup an apache vhost. The home page loads, but assets are still being fetched via http://localhost/mysite

Here’s what id did.
1) on my mac with MAMP, edited my /etc/hosts file and added the below.

127.0.0.1 mysite.local 

2) Then, I added the below to my httpd.conf file

NameVirtualHost *:80 
<VirtualHost *:80>
ServerName localhost
DocumentRoot 
"/Applications/MAMP/htdocs"
</VirtualHost>
<
VirtualHost *:80>
ServerName mysite.local
DocumentRoot 
"/Applications/MAMP/htdocs/mysite"
</VirtualHost

3) then I tried accessing “mysite.local” but httpd threw this error: “Request exceeded the limit of 10 internal redirects due to probable configuration error”

4) So, changed the following in my htaccess.

'RewriteRule ^(.*)$ /mysite/index.php/$1 [L]' => 'RewriteRule ^(.*)$ /index.php/$1 [L]' 

I restart apache, and when I access mysite.local, the homepage loads but interior links redirect to “localhost/mysite”

Any thoughts on the issue?

 
Posted: 18 April 2012 10:46 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

Did you change your base_url in config to be either blank (autodetect) or http://mysite.local/?

This is the htaccess I use for all CI projects:

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

In your vhost entry, you might also need:
Options +FollowSymLinks

 Signature 
 
1 of 2
1