Create Your own MultiSite Script

Converting a PHP script to run multiple web sites from the same code is not all that difficult to do when done the right way. By using this approach you can install the script where ever you like on your hosting and run as many sites as you like sharing as much of the code as you like.

The first thing you need to do is to set up the appropriate directory structure for the site. Since all of the files cannot be common between all of the sites (or they'd all be effectively the same site) we need a way to keep those files that each site needs to have separate in their own folder where it is obvious and easy to work out which site they belong to. The easiest way to do this is to use the domain name of the site as the folder name for the files that are specific to that site.

So our file structure for our multisite version of the script might be:

/
/inc
/img
/example.com
/example.com/inc
/example.com/img
/anotherdomain.net
/anotherdomain.net/inc
/anotherdomain.net/img

With this example the / simply represents the home folder of the multisite which can be anywhere on your hosting and need not be the hosting root folder. The /inc and /img folders (and any others you place there that are not domain names) hold files that are common to all of the domains. Each domain name folder and the sub-folders within it holds those files which are specific to a particular site. These are the files that are not shared with the other sites using the multisite script.

There are several different types of file/data that need to be separate in order for all the sites to actually function separately rather than just being copies of the same site. The most obvious file that needs to be separate for each site is the configuration file that identifies the database the site uses. By specifying the config file in the /inc folder within each domain instead of within the generic /inc folder we can specify completely different configuration options for each site. These don't need to be restricted to identifying which database the site is to use but can set options for anything that your overall script allows to be set via configuration options. We just need to change the call to these files from within the main script so that the file gets read in from the correct place. This requires a simple change to the include or require statement so that it will look in the right folder for the current domain.

require_once './'.$_SERVER["HTTP_HOST"].'/inc/config.php';

Where our script is going to allow people to upload their own files then we need the script to correctly identify the folder that the current domain should use as the destination for uploaded files. For example the following defines the appropriate /pdf folder for the domain when you want visitors to upload PDFs.

define("UPLOAD_DIR", $_SERVER["HTTP_HOST"]."/pdfs/");

Based on these two examples you should be able to easily update the file references for anything that needs to be in the domain specific folders.

The one thing we haven't covered is where specific domains have additional custom web pages for the specific site. Here we take a slightly different approach. By adding a few lines of code into the .htaccess file in the home folder of our multisite we can have the system automatically look in the appropriate domain name folder for any scripts that cannot be found in the home folder.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]

We do need to keep in mind that these scripts are running from a different location than the common scripts even though their URL gives the appearance that they are all running from the same place. So any include references etc will not need to add the $_SERVER reference for domain specific files and will instead need to be coded differently in order to get files from the common /inc folder etc.

require_once '../inc/db.php';

Depending on how your script is designed in the first place, making these changes to convert it to work as a multisite script with most of the code common between sites may involve a small number of changes or it may involve lots of changes. If the changes are going to be needed in lots of places then you might consider using define statements to set values at the top of the script that can then be substituted throughout the script as required. This will make the script easier to maintain in the future.

All that then remains in order to implement your multisite script is to point all of the domains at your multisite home folder.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate