Welcome Guest, Not a member yet? Register   Sign In
Multiple sites from one CI base
#1

[eluser]liquidfire[/eluser]
I've seen a lot of post about running several sites from one CI base. I've been developing a site/service for my company that required the same thing and I would like to share with everyone how I achieved this.

This whole system was inspired by a drupal module. What we want to do is grab the url and then grab only that content on our site that is relevant to that url. Sounds simple right? Well With this setup i fell its very simple and i have yet to find any issues with it. If you can find anything wrong please let me know.

I have not tested this on any other setups. This is what my server is running.
apache2.2.15 php 5.2.13 MySQL version 5.0.91-community and it will require you to have shell access

Step 1
We need to edit our http.conf file. ( i wont tell you how to get there if you dont know this your in over your head sorry.) There are two different setups we can use for this one being for any subdomain and the second for any domain. You will need to alter your virtual host for this.

For Subdomains it should follow this
Code:
<VirtualHost 12.345.678.90:80> // or <VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com *.mysite.com
    DocumentRoot /home/location/to/my/ci/install
  </VirtualHost>

For domains it should follow this
Code:
<VirtualHost 12.345.678.90:80> // or <VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.*.com *.*.com
    DocumentRoot /home/location/to/my/ci/install
  </VirtualHost>

restart apache

Step 2
Time for some early testing.in windows open up command line (mac/linux open terminal) and ping www.mysite.com should work fine. If you ping somethingmadeup.mysite.com you wont get a return.
Now we need to add an A record to the the DNS with the domain of * and the ip to your server. If you use WHM this is very easy. In the side bar search for DNS and go do DNS Functions>> Edit DNS Zone select your site and at the bottom add a new entry.
clear you DNS cache http://docs.cpanel.net/twiki/bin/view/Al...owserCache and now try somthingmadeup.mysite.com and you should get a ping back Smile

Step 3
open application/config/config.php we need to edit the base url so that any controllers that use base_url or anchor() will use the proper domain.
Code:
$config['base_url']    = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'];
pretty simple and straight forward. If the server is https use https://mysite.com else http://mysite.com

Step 4
Well now we have the domain by what good does that do me? Well now that we our domain we can do stuff finally.But some more work is needed before we can do this. I need you to create a database called Sites with the rows of id, url, & uid
Code:
CREATE TABLE IF NOT EXISTS `sites`(
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `url` varchar(200) NOT NULL,
  `uid` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;--

Each site will need its own unique ID(UID) , This UID is what we use to load the content for that site. Urls should not have https or http, IE sub.mysite.com or mysite.com.

Step 4
Ok so now we have a database with a site or two in it but how do I get the data for each specific site? well let me introduce you to getUrl()
Code:
// Get the domain
     $url = $_SERVER['HTTP_HOST']; // this will get sub.mysite.com  or mysite.com
     // get the UID for this specific domain
     // i would also suggest some cleanup here to to help prevent from some type of injection :)
     $query = $this->db->get_where('sites', array('url' => $url));
           $row = $query->row();
            $num =  $query->num_rows(); // isnt used in this example but i wanted to include it.
                                        //  if($num != 1 ){do something here}
            $uid = $row->uid;
            return  $uid;
I also used this in a model so its easy to reference
Code:
$uid = $this->somemodel->getUid();

Step 5
You will need to keep all your content in a database. So i have the controller Content with the function page. this grabs uri segment 1 as the title of the page and loads the content.
Code:
function page(){
   if(strlen($this->uri->segment(1)) > '0'){
       $page = $this->uri->segment(1);
    }
    else $page = 'home'; // this is an example . each site will need its own home page.
                         // so create a function to get the home page for each  site ;)
    
     $data[page] = $this->model->someFunction($page); // step 6

}

step 6
this function I'm not gonna write this function or the database scheme for you for this because each case will be different. But we need to load the content, So as you add content do the database you need to include the uid of that specific site.
Example
Code:
someFunction($page){
$query_page = $this->db->get_where('content', array('uid' => $uid, 'title' => $page));
    $page = $query_page->row();
          
     return $page;
}

This just skims the surface of how detailed you can get this with. other things you may/need to include are items such at site titles, theme, navigation items, meta data etc. this all depends on how you design you "admin" panel and how much detail you want to put into every piece of content for every site. This is a good stepping stone. Best of luck to those of you that want to use this method

Any question comments suggestions please fill free




Theme © iAndrew 2016 - Forum software by © MyBB