Views: 9171
Last Modified: 10.10.2012

Technically, the multisite version of the product allows the following two configuration modes:

  • Mode 1: the software and sites run on a single instance of the Apache web server.
  • Mode 2: each site runs on a separate instance of the Apache web server or a virtual web server.

The product distribution package is shipped configured to operate in the first configuration mode.

We shall consider creating the multiple site system using both configuration modes. The example will explain how to create a system consisting of two sites:

  • www.site1.com - a company corporate web site
  • www.site2.com - a company on-line store

Mode 1. Single web server for all sites.

The first mode presumes the use of a single Apache web server instance whose DocumentRoot is set to /home/www/allsites/.

Install the Bitrix Site Manager 5.x in this directory.

The first mode requires that each site reside in a separate subdirectory of the root folder, for example:

  • /home/www/allsites/s1/
  • /home/www/allsites/s2/

You can choose any other names for catalogs s1 and s2, for example shop and company, or en and de respectively. You can also make one of the sites to reside in the root directory (for example, /home/www/allsites/), whereas the other will be placed in its subdirectory (for example, /home/www/allsites/s2/).

You have to keep in mind that it is extremely important for this configuration mode to separate one site from another and ensure that the file structure does not interfere.

The configuration file httpd.conf of the Apache web server should contain the following lines; this will reflect the chosen configuration:

<VirtualHost *:80>
    ServerAdmin admin@site1.com
    DocumentRoot "/home/www/allsites/"
    ServerName www.site1.com
    ErrorLog logs/allsite.log
    CustomLog logs/allsite.log common
</VirtualHost>

Please note that the record DocumentRoot has the /home/www/allsites/ value which explicitly defines the directory containing the installed software. The variable DocumentRoot has the same value for both sites.

The line <VirtualHost *:80> means that the web server will reply to requests for any domain name and any IP address. That is, having the DNS configured properly, the web server will support both www.site1.com and www.site2.com.

The next step requires that we properly configure and specify both sites in the installed Bitrix software.

You can configure your sites in the Control Panel:

Settings -> System Settings -> Sites.

Click Modify on the site #1 (www.site1.com) and specify the following parameters:

  • Name: site1
  • Domain: www.site1.com
  • Site folder: /s1/
  • Server URL: www.site1.com
  • Site name: Web site of our company
  • Path to web server root folder of this site: leave blank

If DNS is configured in such way that your site is available at http://site1.com address, it is always a good idea to specify the Domain field without www. You can specify as many as needed domain names that users can indicate in their browsers to access your site; each domain name in the list should start from the new line.

Please bear in mind that the values you specify in the Domain field are used by the system to deliver the user information to these domains through the user transfer technology. That is why it is advisable to specify the full list of registered domain names by which your site can be accessed.

Do not include in this list sites that do not run on the current instance of Bitrix Site Manager. Invalid domains will slow down the system response to users; user information will not be transferred to sites running on other instances of the software.

The Site folder parameter should contain the path relative to the root of the catalog containing the public section. The Path to web server root folder of this site folder is not used in this mode; this field should be blank for all sites.

Now set the preferences of the site #2 (www.site2.com):

  • Name: site2
  • Domain: site2.com
  • Site folder: /s2/
  • Server URL: www.site2.com
  • Site name: Our on-line store
  • Path to web server root folder of this site: leave blank

The system is now almost ready to go. It remains only to configure the site selection mechanism in the main project page /index.php.

When site visitors type http://www.site1.com or http://www.site2.com in their browsers, they actually open the /index.php page located in the catalog specified in the DocumentRoot parameter of the web server settings. In the multiple site configuration, this file bears a slightly different purpose: it should contain not the main page of one of the sites, but a piece of PHP code responsible to switch between sites depending on a current domain name.

To accomplish this task, you can use the following functions of the CMainPage class:

  • CMainPage::GetSiteByHost - returns the site ID by the current domain.
  • CMainPage::GetSiteByAcceptLanguage - returns the site ID by the Accept-Language variable in the client browser software.
  • CMainPage::GetIncludeSitePage - returns the fully qualified absolute path to the index page of the folder of the specified site, for further inclusion.
  • CMainPage::RedirectToSite - redirects to the index page of the folder of the site specified.

Here is an example demonstrating an index page of a portal consisting of several sites a site here is defined by the current domain name:

    // include file
    require($_server[?document_root?]."bitrix/modules/main/include/mainpage.php");
    // get the current site ID by the domain name
$site_id = CMainPage::GetSiteByHost(); // get the absolute path to the index page of the site folder $page = CMainPage::GetIncludeSitePage($site_id);
// if the site and index page is defined, then if(strlen($site_id)>0 && strlen($page)>0) {     // include the page
    require_once($page); } else // otherwise {     require($_SERVER['DOCUMENT_ROOT']."/bitrix/header.php");
// we can place the code which will display // if the site has not been defined before } ?> <?require($_SERVER['DOCUMENT_ROOT']."/bitrix/footer.php");?>

In the above example, the domain name typed by a visitor is defined using the CMainPage::GetSiteByHost function. Then, the code compares the domain name with those specified in the Domain field of the site settings to identify the Site ID, and includes the index page from the folder of the appropriate site into the document body using the CMainPage::GetIncludeSitePage function.

In our example, this means that the visitor who opens http://www.site1.com will get the page /s1/index.php in the current page body without redirect. And the visitor who opens http://www.site2.com will get /s2/index.php.

The use of this algorithm allows to avoid redirects for users and search engine bots and simplifies the use of multisite configuration. This algorithm is recommended, but it is not the only possible way of multisite configuration.

Here is an example demonstrating the index page of a portal where one site is in the root directory, the other is in the folder. All the sites have the same host names.

    // include file
    require($_server[?document_root?]."bitrix/modules/main/include/mainpage.php");
    // get the current site ID by the domain name

$mainpage_siteid = CMainPage::GetSiteByHost(); //if the current site is s2, we get the absolute path to the index page of the site folder $page = CMainPage::GetIncludeSitePage($site_id);
if ($mainpage_siteid != "s1" && $page = CMainPage::GetIncludeSitePage($mainpage_siteid)): // include the page require_once($page); die(); endif; require($_SERVER['DOCUMENT_ROOT']."/bitrix/header.php");
// we can place the code which will display // if the current site is s1 which is in the root folder ?> <?require($_SERVER['DOCUMENT_ROOT']."/bitrix/footer.php");?>

So CMainPage::GetSiteByHost() returns a site set as default (on the page Settings -> System settings -> Sites -> List of sites), because host names are the same. And if it is s2, includes /s2/index.php. If not, it runs the following code.

If your sites, in fact, are different language mirrors, another algorithm can be used to identify site. Here, we can use languages supported by the client browser.

Here is an example demonstrating a portal index page consisting of several sites; a site here is defined by the languages specified in the client browser settings:

<?
    // include file with the CMainPage class
    require($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/main/include/mainpage.php");

    // get the site ID by Accept-Language
    $site_id = CMainPage::GetSiteByAcceptLanguage();

    // if the site is defined, then
    if(strlen($site_id)>0)
    {
        // redirect to the site index page
        CMainPage::RedirectToSite($site_id);
    }
    else // otherwise
    {
        require($_SERVER['DOCUMENT_ROOT']."/bitrix/header.php");
        // we can place the code that will display
        // if the site has not been identified before
        ?>
        <?require($_SERVER['DOCUMENT_ROOT']."/bitrix/footer.php");
    } ?> 

In the above example, the function CMainPage::GetSiteByAcceptLanguage checks the languages available in the client browser settings, compares it to the site Language ID and returns the appropriate web site.

After the site has been defined, the CMainPage::RedirectToSite function redirects (HTTP response code 302) and takes a visitor to the index page of the folder of the specified site, for example http://www.site1.com/s1/ or http://www.site2.com/s2/.

Create your own portal index page or choose either of the above examples, and your method 1 multisiting is done.

DocumentRoot

Path to the site root in the server file system is specified in the veb server settings, for example:

  • for Apache - the DocumentRoot parameter in the httpd.conf file;
  • for IIS - the Home Directory -> Local Path tab in the site properties.


Courses developed by Bitrix24