• 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)

[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:


they both are pointing to the same Ci deployment

Any help would greatly appreciated!, thanks in advance


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.

| 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
    $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 .= '/';
    $base_url = 'http://localhost/';
    $base_uri = '/';
// Define these values to be used later on
define('BASE_URL', $base_url);
define('BASE_URI', $base_uri);

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

Something like this in config.php (this is not mine, got from some thread and used succesfully):
$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.

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.

[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!

[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.