41

when I had my site on development environment - it was url: testurl.com

Now on production server my codeigniter app's address has to be someurl.com/mysite/

I moved it there, and everytime I'm trying to run some function, example /home/test - it gets me into someurl.com/home/test - which is WRONG.

It has to be someurl.com/mysite/home/test - How to fix it? I did set

$config['base_url'] = someurl.com/mysite/
pawel
  • 5,976
  • 15
  • 46
  • 68

17 Answers17

68

Base URL should be absolute, including the protocol:

$config['base_url'] = "http://somesite.com/somedir/";

If using the URL helper, then base_url() will output the above string.

Passing arguments to base_url() or site_url() will result in the following (assuming $config['index_page'] = "index.php";:

echo base_url('assets/stylesheet.css'); // http://somesite.com/somedir/assets/stylesheet.css
echo site_url('mycontroller/mymethod'); // http://somesite.com/somedir/index.php/mycontroller/mymethod
Brendan
  • 4,565
  • 1
  • 24
  • 39
  • +1 for your answer on absolute referencing... But looking at this, I have a doubt... Isn't it a bad idea to have the CodeIgniter files AND the 'somesite' files in the same location (that is www)? I mean wouldn't this make the CodeIgniter files accebile by outside bots? Just curious... – itsols Jan 16 '13 at 10:05
  • 5
    Potentially, yes, and there are two things to consider: 1) All CI project files should have `if (!defined('BASEPATH')) exit('No direct script access allowed');` as the first line. This prevents the script from running by itself, because the `BASEPATH` constant is not defined otherwise (be it CI's front controller or another script). 2) You can safely move the `system` AND `application` folders outside of your webroot (www, in your case) as long as you specify their paths in CI's front controller (index.php). Basically your webroot will only contain index.php and assets. Hope that helps. – Brendan Jan 16 '13 at 17:20
  • What is the scenario for overiding this baseUrl – Yasar Arafath Jun 21 '17 at 12:54
22

Check

config > config

codeigniter file structure

replace

$config['base_url'] = "your Website url";

with

$config['base_url']  =  "http://".$_SERVER['HTTP_HOST'];

$config['base_url'] .= preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])).'/';
umar_
  • 491
  • 5
  • 17
Amranur Rahman
  • 1,061
  • 17
  • 29
15

Well that's very interesting, Here is quick and working code:

index.php

/**
 * Define APP_URL Dynamically
 * Write this at the bottom of index.php
 *
 * Automatic base url
 */
define('APP_URL', ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['SERVER_NAME']}".str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME'])); 

config.php

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = APP_URL;

CodeIgniter ROCKS!!! :)

Nono
  • 6,986
  • 4
  • 39
  • 39
5

If you leave it blank the framework will try to autodetect it since version 2.0.0.

But not in 3.0.0, see here: config.php

complex857
  • 20,425
  • 6
  • 51
  • 54
4

I think CodeIgniter 3 recommends to set $config['base_url'] to a full url manually in order to avoid HTTP header injection.

If you are not concerned about it, you can simply add the following lines in your

application/config/config.php

defined('BASE_URL') OR define('BASE_URL', (is_https() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/');
$config['base_url'] = BASE_URL;

In this way you also have BASE_URL constant available in all your project code (including the views) and you don't have to use functions:

config_item('base_url') //returns BASE_URL
$this->config->item('base_url'); //returns BASE_URL
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
  • use SERVER_NAME instead of HTTP_HOST this is more secure – kodmanyagha Dec 26 '18 at 15:03
  • @kodmanyagha AFAIK is not true. In many cases SERVER_NAME is just populated from HTTP_HOST. For max security the only way is to check against allowed host names, see https://stackoverflow.com/a/1459794/260080 – Marco Demaio Dec 26 '18 at 18:49
2

After declaring the base url in config.php, (note, if you are still getting the same error, check autoload.php)

$config['base_url'] = "http://somesite.com/somedir/";

Check "autoload"

CodeIgniter file structure:

\application\config\autoload.php

And enable on autoload:

$autoload['helper'] = array(**'url',** 'file');

And it works.

Raceimaztion
  • 9,494
  • 4
  • 26
  • 41
Vimel Raja
  • 47
  • 4
2

this is for server nd live site i apply in hostinger.com and its working fine

1st : $config['base_url'] = 'http://yoursitename.com'; (in confing.php)

2) : src="<?=base_url()?>assest/js/wow.min.js" (in view file )

3) : href="<?php echo base_url()?>index.php/Mycontroller/Method" (for url link or method calling )

parik
  • 2,313
  • 12
  • 39
  • 67
0

You may use default base URL path for any server That have to used in config folder > config.php files replace you :

$config['base_url'] = 'index.php';

by this code: $config['base_url'] = "http://".$_SERVER['HTTP_HOST']; $config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';

it's auto found your folder form any server

0

application > config > config.php

search for $config['base_url'] and put your site like "//example.com" (skip protocol)

 $config['base_url'] = "//someurl.com/";

This works for me.

Verde
  • 45
  • 6
0

In your config file, leave the $config['base_url'] = ''

base_url will automatically create one. In your Codeigniter system/core/Config.php

        // Set the base_url automatically if none was provided
        if (empty($this->config['base_url']))
        {
            if (isset($_SERVER['SERVER_ADDR']))
            {
                if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE)
                {
                    $server_addr = '['.$_SERVER['SERVER_ADDR'].']';
                }
                else
                {
                    $server_addr = $_SERVER['SERVER_ADDR'];
                }

                $base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
                    .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
            }
            else
            {
                $base_url = 'http://localhost/';
            }

            $this->set_item('base_url', $base_url);
        }

This code is already there to automatically create the base URL and protocol.

Thanks.

Tayyab Hayat
  • 815
  • 1
  • 9
  • 22
-1

Do you miss Double quotation marks?

$config['base_url'] = "someurl.com/mysite"

fuyunbiyi
  • 38
  • 2
-1

Dynamic Base Url (codeigniter)

Just overwrite the line in config/config.php with the following:

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';

-1
$config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/project_name/";

this way you config you base_url , then won't worry about in hosting. both works in localhost and server.

-1
Base url set in CodeIgniter for all url

$config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/";
Siddharth Shukla
  • 1,063
  • 15
  • 14
-1

Change in config.php like this.

$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". @$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;
Anjani Barnwal
  • 1,362
  • 1
  • 17
  • 23
-1

Base URL should be absolute, including the protocol:

$config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/mysite/";

This configuration will work in both localhost and server.

Don't forget to enable on autoload:

$autoload['helper'] = array('url','file');

Then base_url() will output as below

echo base_url('assets/style.css'); //http://yoursite.com/mysite/assets/style.css
Sani Kamal
  • 1,208
  • 16
  • 26
-2

Try to add the controller to your index page aswell. Just looked at as similar structure on one of my own site and I hade my base url set to = "somedomain.com/v2/controller"

  • Can you explain a little deeper, please? – pawel Aug 03 '12 at 08:52
  • By my understanding you have your application and system folder in a folder called "mysite" and as a said I have a similiar structure on one of my own sites. Where I have set my base url to "http://www.somedomain.com/v2/controller", where v2 being my version of your "mysite" folder and controller (or controller/function) being the controller you would like to have as your index page. – pbappia12 Aug 03 '12 at 08:57