user1852176
user1852176

Reputation: 455

CakePHP "down for maintenance" page

I found this post about how to create a "down for maintenance" page but I'm having some trouble getting it to work properly.

define('MAINTENANCE', 1); 
if(MAINTENANCE > 0){
    require('maintenance.php'); die(); 
}

When I place this code in /webroot.index.php it works. However, the answer suggests adding an IP address check so that if it's down, I would still be able to view it and make sure any updates went through smoothly.

So, it would look something like this

define('MAINTENANCE', 0); 
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE'){
    require('maintenance.php'); die(); 
}

The issue is, my IP address will NOT be detected by cake. I've typed echo $_SERVER['REMOTE_ADDR'] and it just shows ::1. I also tried using my user_id but I got the following error Class 'AuthComponent' not found in....

I tried putting this in /index.php and /App/index.php but the maintenance page wasn't triggered and the page loads normally.

Upvotes: 0

Views: 1318

Answers (1)

ndm
ndm

Reputation: 60463

What I normally do is using mod_rewrite, that way it's independent of my application code.

Here's an example, it redirects all access attempts that do not origin from 127.0.0.1 to maintenance.php in the same folder as the .htaccess file:

RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteRule . maintenance.php [L]

The maintenance.php could then look something like this:

<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Retry-After: 86400');

?><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body>
<h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
</body></html>

Note that this doesn't invole an external redirect! I never had any problems with search engines yet, but maybe this was just because the maintenance never took very long, not sure, I'm no SEO expert.

Using an actual redirect could be done using the Redirect flag (and a non-match pattern to avoid a redirect loop):

RewriteRule !^maintenance\.php$ /maintenance.php [R=307,L]

This would redirect to /maintenance.php in case the URL isn't already pointing there.

To make this all even less tied to the application, you could define appropriate rules in the server or virtual host config instead (in case you have access to that), this is what I would prefer, as it's safe to override all application code that way.

Upvotes: 1

Related Questions