Welcome Guest, Not a member yet? Register   Sign In
Multiple domains pointing to same CI application

I am designing multiple location specific sites that are 99% similar. To be more specific, they are business directories that are city specific. I was wondering if it was a good idea to point all the different domains to the same CodeIgniter folder, and based on the URL, select different database that pertains to the specific city.

For example, I have domains:


that would point to the same folder on the server.

Of course, the reason I'm doing this is to increase efficiency if I were to change something on functionality or add new feature. As I would be increasing these sites to over 10 locations in the future, it'd not be practical to update 10 different folders.

So the question is, first of all, is this a good idea? Also, if I were to go from this approach, how would I go about in changing CodeIgniter so that it will select different databases based on the URL. For example, if the URL is NewYorkSite.com, CI would select from the NewYork database. How would I go about doing that?

Can someone please help?

What you could do is detect the url in the config & db config file and have a specific setup for each one. Now you can have the same files but different db behind it. You could also do this trick in the router config and have it point to different controllers.

Detect the url with something like: $_SERVER['SERVER_NAME'];

[eluser]Sarfaraz Momin[/eluser]
There would be 2 way I would prefer ... 1 would be less prefered but still an option. The options are as follows:
1. htaccess = I would have one system folder with different application folders and have ur index.php redirect to the correct application folder based on the URL. The pros would be a totally different codebase and you can play around with them individually without hurting any other site.

2. Routes = I would have the redirects using routes and configure all the databases in my database config file. I would have the correct database group selected based on the URL. The pros would be a single update to reflect on all the domains.

I hope you select the right way of doing it or whatever feel is more optimized but I would definately like to know what you did with it since its quite interesting.

Have a good day !!!

Is this an efficient way of operating multiple sites? Does anyone know how large directories handle their large sites when they have over 50 sites in different locations?

[eluser]Phil Sturgeon[/eluser]
The above methods either do not answer the question or would not work. Routes will not allow you to have multiple sites running on one codebase... well, not by themselves.

The best way to do this is to use Apache proxy which will allow you to stay on your different domains but have a shared single codebase.

You can set up one URL to be the main one. Which will accept parameters like so:


You'll have to work out how to correctly use GET strings mixed with normal params, but that would help out. Or instead of the get parameter, set up a route to use the first CI segment as the location parameter.

Then look at step #1 on my blog post to set up a apache proxy.

You can then do proxy redirects in htaccess or your main httpd.conf file like so:

Quote:RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://masterdomain.com/$1?location=1 [NC,QSA,P]

The flags there are "Non-case Sensitive", Query-String Append and Proxy. This adds the location=1 on the end even if there is already a query string and makes the redirect a hidden one instead of a normal 301 type.

Then you just need to set up either a new htaccess file for each of your domains, or host them all on the same server and modify the httpd.conf for each of your sites (see how to make them virtual hosts in that same blog article).

Thanks for all the replies.

But those steps above still doesn't allow the site to choose different databases based on the domain.

What I mean is that all the controllers, models, and views will be shared by all the different domains. The only thing that is different is the database that would be called based on each domain. For example, if someone goes to http://www.NewYorkSite.com, then the site would use the New York database. If someone goes to http://www.DetroitSite.com, then the site would use the Detroit databse. But they will all share the same controllers, views, models, etc, just different database.

Now, what I was asking is if there is a way to just use different databases based on the domain?

[eluser]Phil Sturgeon[/eluser]
The way I gave you above was a good way to keep your various sites easily manageable and scalable, but if you would like a "JUST databases" solution I have one of those too!

Just make a database config group for each site, then automatically work out which group to use from the server name.

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

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "rsi";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['active_r'] = TRUE;
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_unicode_ci";

$db['www.NewYorkSite.com']['hostname'] = "localhost";
$db['www.NewYorkSite.com']['username'] = "root";
$db['www.NewYorkSite.com']['password'] = "";
$db['www.NewYorkSite.com']['database'] = "rsi";
$db['www.NewYorkSite.com']['dbdriver'] = "mysql";
$db['www.NewYorkSite.com']['dbprefix'] = "";
$db['www.NewYorkSite.com']['active_r'] = TRUE;
$db['www.NewYorkSite.com']['pconnect'] = TRUE;
$db['www.NewYorkSite.com']['db_debug'] = TRUE;
$db['www.NewYorkSite.com']['cache_on'] = FALSE;
$db['www.NewYorkSite.com']['cachedir'] = "";
$db['www.NewYorkSite.com']['char_set'] = "utf8";
$db['www.NewYorkSite.com']['dbcollat'] = "utf8_unicode_ci";

// Grab the current domain config, or if there isnt one use the default
$active_group = in_array($_SERVER['SERVER_NAME'], array_keys($db)) ? $_SERVER['SERVER_NAME'] : "default";


If you do it this way, make sure your htaccess redirects from http://NewYorkSite.com to http://www.NewYorkSite.com too ofc. Thats an easy one:

Quote:RewriteEngine on
RewriteCond %{HTTP_HOST} !^NewYorkSite.com$ [NC]
RewriteRule ^/(.*) http://www.NewYorkSite.com/$1 [L,R=301]

First off I'd recommend running all your sites from one engine. Don't clone your code if you don't have to, it will just be a big mess. Just because you run them off the same engine does not mean you can't dynamically skin them with css etc, so there's very little reason to run multiple instances.

Have a look at the solution to my problem here: http://ellislab.com/forums/viewthread/95977/#485679
My problem is actually a lot harder than yours, so I'll let you know my thoughts on accomplishing what you want.

What I would do is create a pre_system hook.
In this function you can fetch the $_SERVER[‘SERVER_NAME’];
and use a switch statement to define different credentials for different databases, based on the server_name variable.

Then you just dynamically create the database.php config file using these new credentials.
If you look at my thread, what I did had a lot more steps, but you want is much simpler.

Hope this helps.


We currently have this setup for one of our products. Basically what you need to do is pull in the data for each site in dynamically based on the domain.

There are two ways to configure this on the server easily. The first is to use a "Add-on Domain" setup that basically acts like a subdomain. You can create this in cPanel. When you create the add-on domain, create a folder (maybe siteXX) and stick the codeigniter index.php file to launch code igniter. Doing it this way will allow you to have a "base" assets area and a site specific assets area.

The second way is to "park" a domain on the current project. With this method all you need to look for is the URL to pull in the necessary data. Some people mentioned using $_SERVER['SERVER_NAME'] but I believe that will give you problems as it will return whatever the main URL the account is associated with on the server. I've found $_SERVER['HTTP_HOST'] to work like a charm. Here is a quick blog post about the differences SERVER_NAME vs HTTP_HOST

There might be some better setups for doing this. Hope you get the idea though!

Theme © iAndrew 2016 - Forum software by © MyBB