Simple offline maintenance page for websites with .htaccess

March 28, 2014

If you need to do some website or web app upgrades that require you to take the site offline you can use .htaccess so visitors are not seeing broken pages and links. This edit works great for any PHP based site, Wordpress, Codeigniter, Drupal, Expression Engine, you name it.

Create a file called offline.php (or whatever you want) and place it in your root directory and then add this to your .htaccess file.

<code>
RewriteEngine On
RewriteBase /
# for any file/folder that physically does not exist, redirect to offline
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ offline.php/$1
</code>

I you want to get a little fancier you can allow an IP so you can see the updates you are moving over while everyone else is redirected to the offline page.

<code>
RewriteEngine On
RewriteBase /
# for offline use only allow specific ip, use your ip address
RewriteCond %{REMOTE_ADDR} !=127.0.0.01
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/offline.php
RewriteRule .* /offline.php [L]
# redirect everyone else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ offline.php/$1
</code>