• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change the base_url with depending on the domain (two domains pointing to the same CI deployment)

#1
[eluser]Juan Velandia[/eluser]
Hello Everyone, I am trying to use a CI deployment with two different domains, the thing is that I need to change certain elements of the website depending on the domain (ie. logo). Do you have any ideas how to change the base_url according to the domain?

perhaps you would like to have a look:

http://www.segurosinmobiliarios.com.co/
http://www.searrienda.com.co/

they both are pointing to the same Ci deployment

Any help would greatly appreciated!, thanks in advance

JuanK

#2
[eluser]Nick_MyShuitings[/eluser]
I am not sure where I grabbed this from... but this little piece of code has made my life so much easier. It lets me not have to change the base_url variable when I deploy the site, or with subdomains etc. Now, how you take it from there to show a different logo is a different issue... I place this code in my constants.php config file. I use it to make life easier when hosting multiple applications on one codebase.

Code:
/*
|--------------------------------------------------------------------------
| Docment root folders
|--------------------------------------------------------------------------
|
| These constants use existing location information to work out web root, etc.
|
*/

// Base URL (keeps this crazy sh*t out of the config.php
if(isset($_SERVER['HTTP_HOST']))
{
    $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $base_url .= '://'. $_SERVER['HTTP_HOST'];
    $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
    
    // Base URI (It's different to base URL!)
    $base_uri = parse_url($base_url, PHP_URL_PATH);
    if(substr($base_uri, 0, 1) != '/') $base_uri = '/'.$base_uri;
    if(substr($base_uri, -1, 1) != '/') $base_uri .= '/';
}
else
{
    $base_url = 'http://localhost/';
    $base_uri = '/';
}
// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);
define('APPPATH_URI', BASE_URI.APPPATH);

// We dont need these variables any more
unset($base_uri, $base_url);

#3
[eluser]cahva[/eluser]
Something like this in config.php (this is not mine, got from some thread and used succesfully):
Code:
$config['http_https'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';

$config['base_url'] = $config['http_https'];
$config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

That will work with any domain or subdomain that is pointing to your directory in virtual hosts. Plus as a bonus takes account if the url is secure(https) or not.

With that it would be quite trivial to change the logo depending on base_url(or straight with $_SERVER['HTTP_HOST']) on the page for example.

#4
[eluser]Nick_MyShuitings[/eluser]
Yeap, that is virtually identical to what I use... but I kinda like keeping it out of config.php... wherever I snagged it from had a better explination of why.

#5
[eluser]Juan Velandia[/eluser]
Hello Guys, thanks a lot. I'll deploy it during the day and I'll let you know how it worked!

Best regards!

#6
[eluser]Juan Velandia[/eluser]
The code from both of you (cahva and Nick_MyShuitings) worked perfectly, best regards and thanks a lot!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.