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

StackOverflow https://stackoverflow.com/questions/11444121

Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

try this

make this

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

into this

$config['base_url'] = ""

in config.php

It will work fine if you use

<a href="<?php echo base_url('Controller/Function'); ?>">somelink</a>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top