Welcome Guest, Not a member yet? Register   Sign In
base_url() in css and js
#1

[eluser]Felipe Deitos[/eluser]
Hi, i am having a hard time trying to discover what is the best way to build my css and js in the head of my document.

Its a local server, my base url in config.php is:
Code:
$config['base_url'] = 'http://localhost/orca/';

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

My css/js/images are in a root folder called assets.

In my .php file i tryed to put the css 3 ways.
Code:
// 1 - Work in all pages but i dont really want to show the entire url in the source code http://localhost/orca/assets/css/styles.css
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="<?php base_url(); ?>assets/css/styles.css" />


// 2 - This work in pages that i only call the controller ex: http://localhost/orca/home/, if i call http://localhost/orca/home/thing/ the css stop work
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="assets/css/styles.css" />


// 3 - This doest not work at all
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="/assets/css/styles.css" />

I really dont want to use the entire url.. someone know what i am doing wrong?
#2

[eluser]Clooner[/eluser]
[quote author="Felipe Deitos" date="1344861908"]Hi, i am having a hard time trying to discover what is the best way to build my css and js in the head of my document.

Its a local server, my base url in config.php is:
Code:
$config['base_url'] = 'http://localhost/orca/';

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

My css/js/images are in a root folder called assets.

In my .php file i tryed to put the css 3 ways.
Code:
// 1 - Work in all pages but i dont really want to show the entire url in the source code http://localhost/orca/assets/css/styles.css
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="<?php base_url(); ?>assets/css/styles.css" />


// 2 - This work in pages that i only call the controller ex: http://localhost/orca/home/, if i call http://localhost/orca/home/thing/ the css stop work
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="assets/css/styles.css" />


// 3 - This doest not work at all
<link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="/assets/css/styles.css" />

I really dont want to use the entire url.. someone know what i am doing wrong?
[/quote]

If option 1 works all the time why not go with that? Why would you not want to show the entire url in the source of the html? It's as you pointed out the most stable solution. also it's
Code:
<?php echo base_url(); ?>
#3

[eluser]InsiteFX[/eluser]
Simple fix:
Code:
<head>

    <base href="<?php echo base_url(); ?>" />

    // now you should be able to do this
    // 3 - This doest not work at all
    <link rel="stylesheet" type="text/css" media="screen" charset="utf-8" href="assets/css/styles.css" />
</head>
#4

[eluser]Felipe Deitos[/eluser]
Thanks guys, i read some stuffs since i posted this and i guess there is no problem about putting the entire url in those places.

Thanks for your attention!
#5

[eluser]Aken[/eluser]
A beginning slash means start looking from the web root folder.

/assets = example.com/assets
/subfolder/assets = example.com/subfolder/assets

Should tell you why that didn't work.

There's nothing wrong with having the full URL in links to assets. However it could cause conflicts if you want to implement SSL, which will have a different URL. Depends on how you intend to use the site.




Theme © iAndrew 2016 - Forum software by © MyBB