16

Because I do not see Maintenance Mode in the version 1.9 UI System > Configuration, I dropped a maintenance.flag file in the site root. However that also blocks my admin panel access. Can someone tell me how to maintain site maintenance mode with admin access?

4 Answers 4

18

Here's one solution:

Open index.php in root and add (remembering to edit the ‘allowed’ array to contain the IP’s you want to be able to access the site);

$ip = $_SERVER['REMOTE_ADDR'];

$allowed = array('1.1.1.1','2.2.2.2'); // these are the IP's that are allowed to view the site.

then change the line

if (file_exists($maintenanceFile)) {

to

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

Simple. Now you can access the site (admin + front end), while others see its maintenance mode.

Source: http://inchoo.net/ecommerce/magento/maintenance-mode-in-magento/

6

There's handful of extensions that do just that. However they are still just a temporary workarounds as maintenance.flag functionality is still there. To remove it you must be manually edit 'index.php' file which in turn may cause issues with upgrades.

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

This is how 'maintenance.flag' functionality is implemented in 'index.php'. However since you need to edit 'index.php' you could as well do something more elaborate, such as:

if (file_exists($maintenanceFile) && strpos($_SERVER['REQUEST_URI'], '/admin/') === false) {
    include_once dirname(__FILE__) . '/errors/503.php';
    exit;
}

Please note the the code above is quick and dirty hack. You could develop it further, as I just check if '/admin/' exists in url.

1
  • before doing this just check what is the request_uri and modify the condition accordingly. Oct 30, 2019 at 7:59
3

And if your behind a load balancer that is passing the client IP in the HTTP_X_FORWARDED_FOR header make sure to account for it like this:

// account for load balancer that passes client IP
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
   $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
}
if(empty($ip)) {
    $ip = $_SERVER['REMOTE_ADDR'];
}

// whitelist your ips
$allowed = array();
$allowed[]='WHITELIST.IP.ADDRESS.#1';
$allowed[]='WHITELIST.IP.ADDRESS.#2';

if (file_exists($maintenanceFile)) {
    if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) { 
        include_once dirname(__FILE__) . '/errors/503.php';
        exit;
    }
}
1
  • Highly useful for those using an AWS Ec2 load balancer
    – asherrard
    Dec 30, 2015 at 22:49
1

You can set up your own maintenance page and use ErrorDocument 503 to send the maintenance page. Exclude your IP-address via RewriteCond so that you can access the page anyway and don't get redirected:

RewriteEngine On
ErrorDocument 503 /errors/503.php
RewriteCond %{REMOTE_ADDR} !^4.3.2.1 [NC] #your IP
RewriteCond %{REMOTE_ADDR} !^4.3.2.2 [NC] #other IP if needed
RewriteCond %{REMOTE_ADDR} !^127.0.0.1 [NC] #localhost maybe needed depending on server setup
RewriteCond %{REQUEST_URI} !^/errors/503.php
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/css/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !^/admin #your admin path
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*) http://www.yourwebsite.com/errors/503.php [L,R=503]

Please note that it can also be necessary to whitelist additional services like Payment Gateways for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.