I have two application for my project
And here's my config
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/';
If I run site.com, how to get base_url or site_url of api.site.com?
If I run api.site.com, how to get base_url or site_url of site.com?
There is no such function to retrieve the URL in your way. You need to do it manually. Create a helper called base_helper.php
inside helpers
directory and write this code.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
if (!function_exists('url')) {
function url($param = TRUE)
{
if ($param) {
return 'http://site.com/';
}
return 'http://api.site.com/';
}
}
You can call this helpers in controllers/views you wish. Use true
and false
parameter to get main domain or sub domain.
$this->load->helper('base');
echo url();
echo "
";
echo url(false);
Prints
http://site.com/
http://api.site.com/
Hope this helps you. Thanks!!
I hope site.com and api.site.com will have different content. It is best to have two application folders and two different index.php file in each root directory. You can share the same system files, by updating the system path in the index.php file.
If you use following code for base_url then you don't need to change anything you can use this in any server as well as any domain subdomain IP address
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") .
$_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';