0
votes

i'm trying to set up my site that can have different domains for different locales but it should have only 1 codebase. Right now i have to redirect all other domains to the main domain and run my website on that domain. My question is, is it possible to set up my host or my codeigniter application so that i can host my app in all domains without duplicating the codebase. For example : 2 sites www.domain1.com and www.domain2.com should run on the same server, and www.domain1.com/controller/action and www.domain2.com/controller/action should call the same controller file.

Thanks

2

2 Answers

1
votes

All you need to do is set up virtual hosts and point them to the same index file. So, for example, if you were running nginx, you might have something like:

server {
   url domain1.com;
   root /var/www/site/public;
}

server {
   url domain2.com;
   root /var/www/site/public;
}

If you don't know how to set up virtual hosts in your server, then you'll have to look that up in its respective documentation.

1
votes

I'm doing something like that, same code but different databases depending on the subdomain. I have a database with a table for domains, mapping a 'domain' to 'dbname':

    CREATE TABLE IF NOT EXISTS `empresas` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `database` char(10) NOT NULL,
  `active` tinyint(1) NOT NULL,
  `domain` char(255) NOT NULL
  PRIMARY KEY (`id`),
  KEY `domain` (`domain`)
) ;

then an abtract controller base class that makes the magic:

<?php

class AB_Base_Controller extends CI_Controller {

    public $empresa;

    public function __construct() {

        parent::__construct();
        $this->empresa = $this->session->userdata('empresa');

        if ($this->empresa === FALSE || empty($this->empresa)) {
            $this->load->model('sistema_model', 'sistema');
            $this->empresa = $this->sistema->get_empresa();
            $this->session->set_userdata('empresa', $this->empresa );
        }

        // configure the client database
        $db['hostname'] = 'localhost';
        $db['username'] = "whatever";
        $db['password'] = "whatever";
        $db['database'] = 'tqm'.$this->empresa['database']; // the 'tqm' at the beginning is just for make difference with the rest of db
        $db['dbdriver'] = 'mysql';
        $db['dbprefix'] = '';
        $db['pconnect'] = TRUE;
        $db['db_debug'] = FALSE; //TRUE;
        $db['cache_on'] = FALSE;
        $db['cachedir'] = '';
        $db['char_set'] = 'iso-8859-1';
        $db['dbcollat'] = 'latin1_swedish_ci';
        $db['swap_pre'] = '';
        $db['autoinit'] = TRUE;
        $db['stricton'] = FALSE;

        $this->load->database($db, FALSE, TRUE);

    }

}

any controller that inherits that will have the db configured.

the model:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//  CI 2.0 Compatibility
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }


class Sistema_model extends CI_Model
{
    /**
     * Contiene el nombre de la tabla en la que se guardan los datos del dominio y la empresa cliente asociada
     *
     * @var string
     **/
    public $table = 'empresas';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Retorna la empresa basado en los datos de conexión
     *
     * @return array
     **/
    public function get_empresa()
    {
            $query = $this->db->select( 'id, database' )
                    ->where('active', 1)
                    ->where('domain',$_SERVER['SERVER_NAME'])
                    ->get($this->table);

            if( $query->num_rows() == 0 ) redirect('http://www.google.com');

            $result = $query->first_row('array');

            return $result;

    }

}

Sorry by the lenght, but the code will explain better than my poor english.