base_url() gives wrong link if inside href attribute base_url() gives wrong link if inside href attribute codeigniter codeigniter

base_url() gives wrong link if inside href attribute


The url is not returned twice by base_url().If you don't specify the protocol, the browser treats it as a relative link, automatically prefixing it with the current domain and path.

A correct example would be this:

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

According to CodeIgniter docs, it looks like you can also pass the protocol as second argument:https://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url


Note: It's also better to use // in there, so that the SSL won't matter.

Using // is the better method in order to automatically detect which environment it's in and to stay in.

Side note: When running scripts that are meant to be run inside a secure environment, also make sure that there are no http:// references to scripts, images, css, etc., since this will contain mixed environments that some browsers will warn you about it.