Welcome Guest, Not a member yet? Register   Sign In
Losing Styles and Images relative to document path
#1

[eluser]Unknown[/eluser]
How can I combat losing stylesheets / images / etc when changing from the root page (index.php) to a sub page (index.php/view/products)?

I am using a master header for both the root page and sub pages (my headers, css links, etc., are all in this file), but all of my sub pages are no longer relative to the site root (/) and cannot find these files.

If I change "assets/styles.css" to "/assets/styles.css" it doesn't make any difference, which is VERY strange because "../../assets/styles.css" works fine. Why is CI not only changing the CWD but also stopping me from using absolute file paths?
#2

[eluser]Lone[/eluser]
What you need to do is place base_url in front of the link eg.

Code:
<link rel="stylesheet" href="<?=base_url();?>assets/styles.css" type="text/css" />

Remember base_url will already have a trailing slash (as setup in your application/config/config.php file)
#3

[eluser]Nikhil Vijayan[/eluser]
Add the below code between your head tags.. so u will not need to worry about adding base_url function in every css,images assets

Code:
<base href="<?= base_url(); ?>" />

also note that you will have to either load url helper in each controller or better autoload it
#4

[eluser]Unknown[/eluser]
Thank you both, that does it!

For anyone who needs it:
I had taken the first responder's advice and applied the base_url() function before my css and image tags in my header view. I opened up config/autoload.php and added (url) into my helper array. But after the second response, I see I can additionally just throw <base href="<?=base_url();?>" /> into my <head> tag and thereafter don't need to apply anything to my urls, I can just leave them as is.

Solved perfectly, thanks again.
#5

[eluser]cyberbuff[/eluser]
hello, my first post here... thanks. it works perfectly...




Theme © iAndrew 2016 - Forum software by © MyBB