I'm having problem with Codeigniter which duplicates base_url. If i visit index page in controller all url are fine, but when i visit a page which is not index page in controller (cooling in my case) then i get strange duplicated urls like this one http://www.mypage.si/www.mypage.si/services/colling
For example
This is my services controller
class Services extends CI_Controller {
public function index() {
$this->load->view('header');
$this->load->view('main');
$this->load->view('footer');
}
public function cooling() {
$this->load->view('header');
$this->load->view('cooling');
$this->load->view('footer');
}
}
my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Config.php
$config['base_url'] = 'www.mypage.si/';
$config['index_page'] = '';
This is my HTML
<li><a href="<?php echo base_url('services/cooling'); ?>">cooling</a></li>
<!-- results in: http://www.mypage.si/www.mypage.si/services/cooling-->
<img src="assets/images/logo.png" />
<!-- results in: http://www.mypage.si/www.mypage.si/assets/images/logo.png -->
<li><a href="<?php echo base_url()."services/cooling"; ?>">cooling</a></li>
<!-- results in: http://www.mypage.si/www.mypage.si/services/cooling-->
Thank you in advance!
Codeigniter by default will not add the current protocol to your base url, to fix your problem simply update:
$config['base_url'] = 'www.mypage.si/';
to
$config['base_url'] = 'http://www.mypage.si/';
if you want this to be a highly dynamic piece, this is what I currently have as my base url, and it never needs to be updated
$config['base_url'] = "http".((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])) ? "s" : "")."://".$_SERVER['HTTP_HOST']."/";
if you're using an IIS server, this may not produce the same results because the HTTPS
element in the $_SERVER
global does not get filled the same way.
Change this
$config['base_url'] = 'www.mypage.si/';
to
$config['base_url'] = 'http://www.mypage.si/';
I also want to suggest site_url
instead of base_url
on view files. For example:
<li><a href="<?php echo site_url('services/cooling'); ?>">cooling</a></li>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With