Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set proper codeigniter base url?

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/
like image 301
pawel Avatar asked Aug 03 '12 08:08

pawel


People also ask

What is the basic CodeIgniter URL structure?

Basic URL structureclass represents controller class that needs to be invoked. function is the method that is called. ID is any additional segment that is passed to controllers.

What is the difference between base URL and site URL in CodeIgniter?

base_url is without the index_page or url_suffix being appended. like site_url , you can supply segments as a string or an array. If you want a URL access to a resource use base_url you can supply a string to a file, such as an image or stylesheet else site_url is enough.


3 Answers

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
like image 81
Brendan Avatar answered Sep 29 '22 16:09

Brendan


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'])).'/';
like image 22
Amranur Rahman Avatar answered Sep 29 '22 17:09

Amranur Rahman


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!!! :)

like image 13
Nono Avatar answered Sep 29 '22 17:09

Nono