Search code examples
phpapachemod-rewriteurl-routing

How to layout a multisite application with shared codebase


4 Websites share the same codebase, each has a unique domain. Some functions may differ depending on the domain, and the same applies to CSS, Javascript and image files.

Now what is the preferred way to get the information in php, which domain is requested? Could anyone point out advantages/disadvantages of the proposals below, or make a better proposal?

Solution 1: Point all 4 domains to the same index.php, and get the domain via $_SERVER['SERVER_NAME']

Solution 2 Like 1., one index.php for all domains, but pass the domain as query parameter via mod_rewrite:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{HTTP_HOST} ^(.*) [NC] RewriteRule ^ index.php?site=%1 [QSA,L]

Solution 3: Point the 4 domains to 4 different index php, maybe in 4 different public folders, and define the domain on top of these index.php files.

This may sound trivial or like it doesn't matter much, but I want to be sure to pick the best solution.


Solution

  • We've done this before, with a framework that we built for building websites, which supports using the same installed codebase for N number of sites. We do this by:

    • Have all domains point to the same index.php file within the same DocumentRoot.
    • Have all requests for all pages point to the index.php using .htaccess and rewrite rules.

    Then the index.php file sets up the environment. We have a database table that links the result of $_SERVER['SERVER_NAME'] with a one-to-many relationship to the site being requested. This way, a request to example.com and www.example.com can reference a single site, but example2.com could reference a different site. Once the index.php file determines the site and page being accessed, it grabs relevant information for that request from the database, then creates an associative array called $site[] which stores all of the information for the request being made. This array is then used to determine what assets to read in, like a specific template from the CRM, and which assets to read in that are for that specific site and page (like CSS, JS, etc).