1

Here's the puzzle:

I have several individual domains hosted on a single Apache server. They are related, look and laid out similarly, but have individualized specific content.

I have written a code "framework" which is to be shared among the domains, with the much of the content actually filled from a database. Even static pages with shared content (About Us, Privacy Notice, etc.) have variables to be filled per site-specific, such as the site name, and the logo etc., some of these variables filled via domain-specific config files.

What I want to do is:

  1. Have a single shared database to serve all the sites for the site-specific content. The database will also host the users and their content. I have been told by my host this is possible, and this is perhaps the easy part. I am using PHP MySQL. I presume I would make a standard remote connection to the database.

  2. Find the best way to share the code framework. I hate the idea of copying the same code to a dozen or more sites. But, whatever must be done. If I can share the code somehow, or if I can figure out some tools to upload automagically...

So the main answer I am looking for is how to share the codebase without uploading it twelve times. If possible. One possibility seems to be scripted FTP. But that seems archaic. Seems there should be some elegant yet secure way to share code among self-owned domains on a single server.

Hoping to harvest some ideas...any takers?

Thanks

2
  • Have you written that framework yourself, or is it some 3rd-party thing? If 3rd party, read the documentation. If you've written it yourself, add a column site to each of the tables where it's relevant, make site=NULL in a row mean it's for all sites, add ... and site=$currentsite or site = NULL order by site NULLS last to every select on those tables. Feb 5, 2014 at 19:53
  • Yes, I have written the framework myself. Thanks for the suggestion. I will try to re-write my question to clarify its scope. Feb 5, 2014 at 20:57

3 Answers 3

1

This will really depend on the level of similarity all your sites share.

Based on your question, it sounds like they are very similar, meaning you can simply have ALL sites point to the same codebase, and use the $_SERVER[HTTP_HOST] serve as a conditional that will serve up the appropriate content either like:

switch ($_SERVER[HTTP_HOST]) {
    case 'domain1.com':
        // do something
        break;
    case 'domain2':
        // something else
        break;
}

Or in the DB:

SELECT TextBlurb
FROM Content
WHERE page = "landing.php"
AND site = "$_SERVER[HTTP_HOST]"
1
  • suppose i have a website which displays similar content, so as per your solution if i wish to proceed: How can I go ahed? Firstly I need to get the server for all domains i have purchased? Or how the single codebase will work? I am really not clear.. Can you help me understand this query. Thanks a lot Apr 14, 2020 at 16:56
1

You will need to have a database that has a site context. This isn't hard. Have a table with an entry for each site. ID|Site Name

Then, in user tables, article, tables, page tables, etc... you can have a column that specifies the site context using the ID in the first table. This way each site can be configured with an ID, and all queries automatically use this id, to get content for their site only.

So a single DB is not hard.

Sharing the code base is also simple. So long as you update the code base generically, and make sure core code stays site agnostic, you can easily copy the same framework to each site, and then easily update each one with updates. Since core code is site agnostic, you can easily patch your framework, without overwriting any site specific settings/code.

Hope this helps.

EDIT:

If one single code base is a must, then you need a catch at the beginning of every request to see what domain is being called, and then set the site ID accordingly. This way everything else will use that site ID to run queries, load styles, views, etc...

However, being in the web industry for over a decade, and specializing in custom built CMS system, I strongly recommend you do not opt for a solution that uses the exact same code for different websites.

It makes debugging harder, and it makes bugs in your code more potentially catastrophic. It also makes hacking your system a breeze and a bigger target since someone can simultaneously take down 12+ sites by hacking one code base.

What you want to do can be done, but I would recommend not doing it.

2
  • Thanks for the effort. I agree. The DB part is easy. But I would really love to avoid uploading the code twelve times. I am truly hoping for some way to have the code on the server in one place for the domains to share. Feb 5, 2014 at 21:09
  • Edited my OP for you.
    – Kovo
    Feb 6, 2014 at 16:40
0

To share code among several domains - have one central domain, and then park the rest on that domain.

If using cPanel, there would only be one active domain and one cPanel for it, and in cPanel is where you find the functionality to park domains. I previously had several active domains each with their own cPanel. I had to terminate the domains in WHM, and park them on my central domain. Then I could share the underlying code, using configuration files based on reading $_SERVER['SERVER_NAME'] to tell me what domain they are visiting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.