A maintenance page comes in handy to inform users about planned downtime of a web application.
This posts explains how to implement a simple maintenance page using Apache’s .htaccess together with mod_rewrite. This setup has also been tested using vhost settings.
- Define your (vhost) settings, to allow .htaccess to override settings used by mod_redirect:
<VirtualHost *:80> ServerName name.localhost DocumentRoot "/full/path/to/web" <Directory "/full/path/to/web"> AllowOverride All Allow from All </Directory> </VirtualHost>
- Define redirect rule in .htaccess:
<IfModule mod_rewrite.c> RewriteEngine On #RewriteBase / RewriteCond %{DOCUMENT_ROOT}/maintenance -f RewriteCond %{REQUEST_FILENAME} !(maintenance.jpg)$ RewriteRule .* maintenance.php [L] </IfModule>
- Write the maintenance.php file:
<div style="text-align: center; padding-top: 50px;"> <h2>Maintenance...</h2> <p><img title="maint" border="0" alt="maint" src="maintenance.jpg" /></p> </div>
In order to enable the maintenance mode simply create a file called maintenance inside your document root. The .htaccess will detect this file when a request is received and will check if either the maintenance image is requested (which will be served as usual) or any other page, which will be redirected to the maintenance page.
Hint: You might want to include an automatic logout section in the maintenance page, so that in case users with active sessions will be logged out gracefully when requesting pages with the maintenance mode enabled, e.g.:
<?php session_start(); $_SESSION = array(); if(isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time() - 42000, '/'); } session_destroy(); ?>