4

I created a CMS page on magento for maintenance page but I need to exempt the IP's of the developer so we can test while the other viewing the maintenance page that I've created.

I activate the maintenance cms page under admin. I make the maintenance page as the home page but since we need to access the front end side (exempt some IP) how can I do it without putting maintenance.flag on the root?

The problem with the maintenance.flag is, that the API should be still accessable.

4 Answers 4

2

As others have suggested, you can patch index.php to whitelist IPs from maintenance mode. My usual solution to that is to only patch the file once and retrieve the whitelist from maintenance.flag itself, which feels less dirty and actually makes it less error-prone:

Replace

if (file_exists($maintenanceFile)) {

with

if (file_exists($maintenanceFile) && !in_array($_SERVER['REMOTE_ADDR'], file($maintenanceFile)) {

Then add any IP that should be whitelisted to the maintenance.flag file, separated by line break:

127.0.0.1
10.0.0.1

Since you were asking for a non-IP based solution that whitlists all API calls, I suggest this additional check at the same place of index.php:

$isApiRequest = preg_match('#^/api/#, $_SERVER['REQUEST_URI']);
if (file_exists($maintenanceFile) && !in_array($_SERVER['REMOTE_ADDR'], file($maintenanceFile) && !$isApiRequest) {

Since as long as you did not create additional rewrite rules for the API, all API entry point start with /api/ you can determine by the URI if the current request is going to the API.

1
  • 1
    This is solid, I had a couple of little tweaks in mine though. My API client was hitting /index.php/api/, plus you should pass FILE_IGNORE_NEW_LINES to file. Also a missing ' and ')' in your answer - but worked well for me - thanks! Commented Apr 29, 2015 at 5:55
0

Just put a maintenance.flag in the root. Instead of creating a CMS page edit errors/default/503.phtml and put your stuff in there. Then edit index.php:

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

and edit your IPs as condition to the IF. Make sure that you didn't do something wrong and check e.g. with your phone.

4
  • there's a problem when I put maintenance.flag because some of the web service of my site cannot access it example is the SOAP Commented Oct 1, 2014 at 7:23
  • Thats part of maintenance :D When you know the systems which connect to your site, you can add their IPs as well. If not, this is not the way for you to go. Commented Oct 1, 2014 at 7:26
  • I see. is there any other way? Commented Oct 1, 2014 at 7:28
  • You need to somehow split your users in two groups: api access and "real" users. I have no idea how to do this. Check the requests, hopefully you find something which can be used. But my experience with the API is too small to answer this question Commented Oct 1, 2014 at 7:30
0

Point your API Requests calls to api.php instead of index.php and avoid the if condition for the maintenance file check in index.php.

As for the CMS Page, as Fabian mentioned it would be ideal to create it as a static .html page within the root of the store and simply change the reference within your index.php accordingly. The alternative would be adding a condition to the if (file_exists($maintenanceFile)) { in the index.php file but this just complicates the process, but may be ideal for your situation as your wanting to whitelist IPs.

More details on the alternative:

$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)) {

Taken from

0

Magento extension: Easy Site Maintenance (with cms content)

with all futures with ip address manage.

enter image description here

0

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.