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

25,347

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.

Share:
25,347
dave
Author by

dave

Updated on March 11, 2020

Comments

  • dave
    dave about 4 years

    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?