Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter duplicates base_url()

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!

like image 601
Valor_ Avatar asked Nov 17 '15 18:11

Valor_


2 Answers

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']."/";

NOTE:

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.

like image 121
iam-decoder Avatar answered Sep 25 '22 02:09

iam-decoder


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>
like image 28
Deniz B. Avatar answered Sep 24 '22 02:09

Deniz B.