CodeIgniter Forums
Second display gives 404 error on style sheet - 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: Second display gives 404 error on style sheet (/showthread.php?tid=49950)

Pages: 1 2


Second display gives 404 error on style sheet - El Forum - 03-08-2012

[eluser]Ed Robindon[/eluser]
I'm trying to do a simple site with an application wide template view:
The view template:
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; &lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;link rel='stylesheet' href='assets/styles/site_style.css' /&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="wrapper">
<div id="header">
&lt;?php echo $page_title; ?&gt;<br/><br/>
<a href="home/about">About Us</a>
</div>
<div id="content">

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

The controller:
Code:
&lt;?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




Second display gives 404 error on style sheet - El Forum - 03-08-2012

[eluser]CroNiX[/eluser]
Don't use a relative path for your stylesheet, or anything else.


Second display gives 404 error on style sheet - El Forum - 03-08-2012

[eluser]Ed Robindon[/eluser]
Thanks, I'll try it.
Still don't understand why it would work from the index function...


Second display gives 404 error on style sheet - El Forum - 03-08-2012

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


Second display gives 404 error on style sheet - El Forum - 03-08-2012

[eluser]InsiteFX[/eluser]
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; &lt;?php echo $title; ?&gt;&lt;/title&gt;

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

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

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

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



Second display gives 404 error on style sheet - El Forum - 03-08-2012

[eluser]Ed Robindon[/eluser]
Thanks for the help guys.

The base_url addition made everything better...




Second display gives 404 error on style sheet - El Forum - 04-18-2012

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

Code:
&lt;link rel='stylesheet' href='&lt;?php echo base_url();?&gt;assets/styles/site_style.css' /&gt;

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


Second display gives 404 error on style sheet - El Forum - 04-18-2012

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


Second display gives 404 error on style sheet - El Forum - 04-18-2012

[eluser]InsiteFX[/eluser]
Code:
&lt;link rel="stylesheet" href="&lt;?php echo base_url('assets/styles/site_style.css');?&gt;" /&gt;



Second display gives 404 error on style sheet - El Forum - 04-18-2012

[eluser]CroNiX[/eluser]
Code:
&lt;link rel='stylesheet' href='/assets/styles/site_style.css' /&gt;
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.