Magento 2: Maintenance Mode

For activating maintenance mode with IP addresses execute below command in CLI

php bin/magento maintenance:enable --ip="192.168.0.52" --ip="192.168.0.86"

the above command will automatically create a .maintenance.flag and .maintenance.ip file under root/var folder.

and .maintenance.ip the file contains the above two IP addresses separated by a comma.

For more Information, you can find from devdocs.

The above information is for maintenance mode.

To redirect a custom page while maintenance mode writes below code in root/index.php.

$maintenanceFile =  __DIR__ . '/var/.maintenance.flag';

if (file_exists($maintenanceFile)) {
    header('Location: http://127.0.0.1/m2ee/Error.php');
    die();
}

If anyone has better approach please update this answer.


The Maintenance Mode check-in lib/internal/Magento/Framework/App/Bootstrap.php. This file calls to lib/internal/Magento/Framework/App/MaintenanceMode.php. If .maintenance.flag file exists in var folder, it returns false.

If you want the redirect to a new page when maintenance enables, you need change lib/internal/Magento/Framework/App/Bootstrap.php file. Add this code below after line 288:

header('Location: http://127.0.0.1'); //your page
die();