How to make multiple website(subdomain of your main site) in codeigniter with same code and database dynamically

In this tutorial I will explain you how to make multiple website(subdomain of your main site) in codeigniter with same code and database dynamically.
I hope you are little bit faimliear with multiple website.In multiple website we create the subdomain of the website and use the same database and same code structure for all the subdomain.
Like i have main domain http://www.example.com , want to create subdomain
like http://www.demo1.example.com, http://www.demo2.example.com, http://www.demo3.example.com, likewise.

We must want to use same codebase, database. Only the subdomain different.

For that first you need to contact your hosting provider and need to activate multiple website functionality on your website.

Now create a table in your database where we will insert the subdomain data using form.

CREATE TABLE IF NOT EXISTS `subdomains` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `subdomain_name` varchar(100) NOT NULL,
 `email` varchar(50) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Now create a view to insert the subdomain name in the database and insert data in your subdomain table dynamically. I hope you already know how to insert data in database in codeigniter.So here I will not write down the code to insert the subdomain data in database.
If you don’t know how to insert data in database in codeigniter please read this tutorial
how to insert data in database in codeigniter

Now you need to make the base_url dynamic in config.php file.
open config.php file and replace your base_url to

if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on"){$ssl_set = "s";} else{$ssl_set = "";}
$config['base_url'] = 'http'.$ssl_set.'://'.$_SERVER['HTTP_HOST'];

Now run your main domain http://www.example.com this will redirect you to the your main site.

to check the subdomain is it registered or not in your database you have to do some coding in your controller.

when your first controller run(the controller you have set default controller in your route.php file).
Lets Mycontroller is your default controller.

write this code in the controller construct function so we can check before running any function that the subdomain is available or not in your database

 

  1. <?php
  2.  
  3. class Mycontroller extends CI_Controller {
  4.     public function __construct()
  5.     {
  6.         parent::__construct();
  7.         // Your own constructor code
  8.         $subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2); //it will create the various parts of domain
  9.         $subdomain_name = $subdomain_arr[0]; //this will get your subdomain name to check if it is available or not
  10.        // echo $subdomain_name;
  11.         $this->db->from('subdomains')->where('subdomain_name', $subdomain_name);//now check for the subdomain if it is available
  12.         $query = $this->db->get();
  13.         if($query->num_rows() < 1)
  14.         {
  15.         //redirect ('make error page to show domain name not avalable');//if there is no result in your database show the error to user that subdomain
  16.         //is not available
  17.         echo "domain name not registered";
  18.         }
  19.     }
  20.      function index()
  21.     {
  22.         $subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
  23.         $subdomain_name = $subdomain_arr[0];
  24.          
  25.         $this->db->from('subdomains')->where('subdomain_name', $subdomain_name);
  26.         $query = $this->db->get();
  27.         $subdomain_info = $query->row();// here you will get the subdomain information
  28.          Print_r($subdomain_info);
  29.          echo "domain registered";
  30.     }
  31. }

 

Now add data in your table (make a form to insert the subdomain data in database. ).
For example you have entered domain name demo1 in table as subdomain name.
Now run http://www.demo1.example.com,

If the subdomain is available in database then your function will run and it will get data according to your domain name and print it.If domain name not found it will redirect you to error page that domain name is not available.
Please note that you need to save the subdomain ID in each table to differentiate the data of each subdomain later on.

Hope this article how to make multiple website(subdomain of your main site) in codeigniter with same code and database dynamically will help you.Feel free to ask any question.
You can email me to get the complete code how multiple website work in codeigniter. Keep reading. cheers!