codeigniter: why is that when i echo base_url() in an href attribute of an anchor tag, it echoes twice

dave picture dave · Jul 12, 2012 · Viewed 24.3k times · Source

so basically when i echo the codeigniter function base_url() in the href attribute of an anchor tag, it appears to echo it out twice. Example:

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

and the above, if you inspect it your chrome browser shows this:

<a href="www.mysitedomainname.com/www.mysitedomainname.com/">somelink</a>

"mysitedomainname.com" is just a name i made up for this example. Any reason why this is happening?

Answer

Sergey Telshevsky picture Sergey Telshevsky · Jul 12, 2012

There are three reasons I'm aware about that can cause this.

The first one is when something wrong is written in config.php on line 17 $config['base_url'] = ''; - it better be left empty, just like when you download CI.

The second one is if you have set $config['base_url'] value to something without prefixing it with http:// or other protocol.

The third one is if you have set base href somewhere:

<base href="http://www.mysitedomainname.com/" />

When you need to link to some other page, you should use site_url(), base_url() can be used to link stylesheets, js, img src attributes and other real URL's. The reason is pretty simple, base_url() does not include the index_page value set in config.php.