Mini Tutorial: Improving Magento’s Maintenance Mode for Store View-Specific Use

When implementing significant changes to your Magento store, it is important to maintain ongoing communication with the customer. You may want to temporarily enable “Maintenance Mode” in order to prevent access to your store while under a state of construction. This practice is beneficial as it informs the end user – your customers – that changes are currently being applied to your Website. Communicating with your customer in this fashion will enhance customer satisfaction, as simply performing maintenance on your store without notification can potentially disrupt their shopping experience.

Magento, however, by default, enables Maintenance Mode on the entire Website. This is not preferred if you’re implementing changes only to a subset of Store Views on your Website – for instance a version of your store that’s offered in another language. In this tutorial, you’ll learn how to limit the scope of Maintenance Mode, customize the page that your customers see while in Maintenance Mode, and retain access to your Magento Admin Panel.

Activating Maintenance Mode

Normally, to activate Maintenance Mode you just need to place a maintenance.flag file in your root Magento directory. Its presence causes the following conditional statement in the index.php file to evaluate as true and the included statements are then executed, which displays the Magento default “503 Service Unavailable” page.

$maintenanceFile = 'maintenance.flag';
if (file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . '/errors/503.php';
    exit;
}

As stated earlier, this enables Maintenance Mode for all users – including yourself as the administrator user.

Whitelisting the Admin

To prevent this behaviour, add the following code to the index.php (be sure to make a backup of your index.php file first).

$ip = $_SERVER['REMOTE_ADDR'];
$allowed = array('127.0.0.1');

As an example, we have only included the localhost IP address in the $allowed array of IP address. However, you should include here any IP address that you want to “whitelist” or be exempt from being redirected to the Maintenance Mode page, such as your current IP address.

$_SERVER[‘REMOTE_ADDR’]; in the code above is the IP address from which the user is viewing the current page. Next, we proceed to change the following line in index.php from

if (file_exists($maintenanceFile)) {

to

if (!in_array($ip, $allowed) && file_exists($maintenanceFile)) {

This small change causes the “503 Service Unavailable” page to display only when the maintenance.flag file is present AND the current user’s IP address is not whitelisted.

Limiting the Scope of Maintenance Mode

The purpose of limiting the scope of Maintenance Mode assumes that you already have a properly configured multi-site, multi-domain setup with multi-store or multi-language retailing all supported from the same backend. As such, the instructions for preparing that configuration are outside the domain of this tutorial. However, to better understand the remainder of this article, we are going to take a quick look at that existing configuration.

Now that you can access your Magento Admin Panel, login to your account and navigate to System > Manage Stores. From here, click on the store view you are implementing changes on and would like to limit Maintenance Mode to. Take note of the value for the ‘Code’ attribute.

In your store’s VirtualHost definition you should see the following:

SETENV MAGE_RUN_CODE "default"
SETENV MAGE_RUN_TYPE "store"

Where, in this example, default is the value of the ‘Code’ attribute mentioned earlier. With this information in mind, let us return to editing the index.php file.

Wrap the recently modified Maintenance Mode conditional statement in the following conditional statement:

if ((isset($_SERVER['MAGE_RUN_CODE']) && $_SERVER['MAGE_RUN_CODE']
  == 'default') && (isset($_SERVER['MAGE_RUN_TYPE']) &&
  $_SERVER['MAGE_RUN_TYPE'] == 'store')) {
    ...
}

This further limits the application of Maintenance Mode to one specific Store View. Note that default and store match the VirtualHost definition for the store. This condition can be easily expanded to include multiple stores.

Creating a Custom Maintenance Page

You may want to create a custom “Under Construction” page for your website in order to display some basic site information during the renovation that the default “503 Service Unavailable” doesn’t include.

In your Magento root directory, create a maintenance subfolder. At a minimum, include a file called maintenance.php here. As we are creating an ordinary HTML landing page, you can add further subdirectories and files to the maintenance folder, as you deem necessary (such as for the inclusion of style sheets, images, or JavaScript files). The design of your landing page is up to your discretion and beyond the scope of this tutorial.

Back in index.php, replace include_once dirname(__FILE__) . ‘/errors/503.php’; with:

include_once dirname(__FILE__) . '/maintenance/maintenance.php';

Proceed to design and develop your maintenance page as required.

Wrapping Up

The code we have produced within index.php should look like:

if ((isset($_SERVER['MAGE_RUN_CODE']) && $_SERVER['MAGE_RUN_CODE']
  == 'default') && (isset($_SERVER['MAGE_RUN_TYPE']) &&
  $_SERVER['MAGE_RUN_TYPE'] == 'store')) {
    $maintenanceFile = 'maintenance.flag';
    if (file_exists($maintenanceFile)) {
      include_once dirname(__FILE__) .
        '/maintenance/maintenance.php';
      exit;
    }
}

This code can be adapted to include different flags, conditions, and display pages for different store views that are in Maintenance Mode at the same time. Another suggestion, for instance, would be to break a string into an array and compare a subset of the array elements in order to add a range of IP address to the whitelist.

Use Magento’s Maintenance Mode to your advantage to keep your customers informed, ensure optimal performance, and reduce downtime while implementing changes to your Website.