Simple Site Maintenance Page via .htaccess

I wanted to post this as I often forget those code snippets I use only a few times each year.  This simple addition to the .htaccess file will allow you to display a page to people coming to your site while letting you still see the site normally.

RewriteEngine On
RewriteCond %{REQUEST_URI} !/maintenance.html
RewriteCond %{REMOTE_HOST} !^10\.10\.10\.10
RewriteRule $ /maintenance.html [R=302,L]

Simply replace the 10s with your IP and if desired change the name of the file you want to display (2X).  If you already have “RewriteEngine On” in your .htaccess file be sure to remove that part as it will be redundant.

The first line simply ensures that you don’t redirect forever.  The second ensures that you won’t redirect yourself, or others at your public IP address.  The last will send everyone else to your maintenance message, in this case at /maintenance.html.

Also, if you want users to still be able to access your site’s asset files you can add something like:

RewriteCond %{REQUEST_URI} !(\.gif$)|(\.jpg$)|(\.png$)|(\.css$)|(\.js$)

to the beginning of the rewrite rule which will ignore the redirect to maintenance.html for image, css and javascript files.