Putting website into maintenance mode

When you have dynamic website with loyal users you should provide at least decent support and service for them. One of things you have to do when working on startup is maintenance of service, adding new features and so on …. How to do it right?

How to create good maintenance mode?

First of all do not let your users to lost some of their activity on site, when you are putting your website into maintenance mode you should ensure that all GET/POST request will be served when website will be back online.

How to do it? There are many ways to achieve that, prefer using .htaccess with additional php file to keep user state.

Here is draft of my solution (insecure draft) - before using it you should tune code below to your needs, this article should be treated like inspiration, not a solution to your problems.

Before performing this operations make sure that you have mod_rewrite enabled:

sudo a2enmod rewrite # enables mod rewrite

The .htaccess maintenance mode file:

## make sure nobody gets the htaccess files
<Files ~ "^[\._]ht">
   Order allow,deny
   Deny from all
   Satisfy All
</Files>

ErrorDocument 503 /maint.php
<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_URI}  !^/maint.php$
   RewriteRule .* /maint.php [NC,L]
   ErrorDocument 503 /maint.php
</IfModule>

The maint.php file:

<html>
<head>
<title>Site is under maintenance</title>
</head>
<body>
<?php
   #echo '<pre>'.print_r($_SERVER, 1).'</pre>';
   if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) {
      echo '<a href="'.$_SERVER['REQUEST_URI'].'">Reload</a>' ;
   } else if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
      echo '<pre>POST\n\n'.print_r($_POST, 1).'</pre>';
   }
?>
</body>
</html>

For more information about mod_rewrite check out:

Comments

comments powered by Disqus