Chief Makii
Chief Makii

Reputation: 121

Codeigniter URL and base_url() confusion

 $config['base_url'] = 'localhost/project';

 <link href="<?= base_url(); ?>/css/bootstrap.min.css" rel="stylesheet">

Would produce an error and it won't load my css on my view because of double '/'.

This is the pagesource of the view on a browser.

<link href="localhost/project//css/bootstrap.min.css" rel="stylesheet">

And the css won't load. When i try the page source and follow the link, it leads to 404 error.

What i'm confused about is that when i add protocol on my base_url on config file.

<link href="http://localhost/project//css/bootstrap.min.css" rel="stylesheet">

For reasons unknown, the above url seems to point to the css and it loads on my view even though it still has double '/' on the url between 'project' and 'css'. Also, even if i remove the '/' before the 'css' on the view, it will load, so long as i add a protocol on my base url even though without the protocol even with no double '/' it still won't load the css.

<link href="http://localhost/project/css/bootstrap.min.css" rel="stylesheet">

I also have some questions,

On my config base url above, i never put an '/' at the end so why does it add an '/' when i use the function base_url on my view. Why is it like this?

My 2nd question, is that the '/' on my url.

<link href="css/bootstrap.min.css" rel="stylesheet">

<link href="/css/shop-homepage.css" rel="stylesheet">

The first link when clicking on the url on the view pagesource on my browser, it still has the controller in the url.

http://localhost/project/Pages/css/bootstrap.min.css

While if i add a '/' before the 'css' it removes the 'project' and 'Pages' in the url.

http://localhost/css/shop-homepage.css

I could solve the problem just that i don't know why they occur so even though i can get my css to load i don't feel right because i don't know why.

Upvotes: 0

Views: 1839

Answers (3)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146540

You simply can't start a URL with the hostname:

localhost/project

Otherwise, it'd be impossible to tell out whether you mean a domain name or a location within your server.

You need a full protocol prefix:

http://localhost/project

... or, if already in the context of HTTP, at least the double slash:

//localhost/project

Browsers often prefer to hide the actual URL (thus the widespread confusion) but the underlying value works exactly the same.

Upvotes: 0

Phiter
Phiter

Reputation: 14982

The base_url is part of the URL Helper.

The problem is in our config file, the configuration file says that:

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = 'http://localhost/site/';

You should have added the slash, and since you didn't, the base_url() function adds it automatically.

The best usage is to provide the file location as a parameter, as of

<link href="<?= base_url("css/bootstrap.min.css"); ?>" rel="stylesheet">

Upvotes: 1

Just_Do_It
Just_Do_It

Reputation: 821

Ideally your base_url in the config should be like:

$config['base_url'] = "http://www.example.com/";

and in your view you can add folder name and file name that you need to include.

<link href="<?= base_url(); ?>css/bootstrap.min.css" rel="stylesheet">

This way there will be no confusions or double '/'

Plus prefer using this:

<link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet">

Upvotes: 0

Related Questions