A fresh mix of code snippets and tutorials

Maintenance mode via .htaccess

For WordPress users there are at least three great plugins that make maintenance mode just dead-simple. Even so, here is how to do it with .htaccess.

To enable maintenance mode for your site, place this code at the top of your site’s root .htaccess file:

<IfModule mod_rewrite.c>
	RewriteEngine On

	# local ip
	RewriteCond %{REMOTE_ADDR} !^123.456.678

	# server ip
	RewriteCond %{REMOTE_ADDR} !^111.222.333

	# w3c validation
	# RewriteCond %{REMOTE_ADDR} !^128.30.52.

	# maintenance page and assets
	RewriteCond %{REQUEST_URI} !/maintenance [NC]
	RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]

	# serve default 503 response
	RewriteRule .* http://example.com/maintenance.html [R=503,L]
	# alternate response without custom page
	# RewriteRule .* - [R=503,L]

# serve custom 503 response
ErrorDocument 503 /maintenance.html

# alternate response without custom page
# ErrorDocument 503 "Coming soon!"

<IfModule mod_headers.c>
	# 3600 = 60 minutes
	# 86400 = 1 day
	# 604800 = 1 week
	Header always set Retry-After "86400"

Some notes about this snippet:

  • Edit the IPs in the first container with your own values
  • The W3C Validator is not allowed unless you uncomment its line
  • Change example.com with your own domain name
  • Edit the Header directive with any amount of time
  • The lines “alternate response without custom page” may be used instead of the custom maintenance page (makes it a bit easier)

Then, with that code in place, your server is going to respond with the file, maintenance.html, if it exists. You can either delete the ErrorDocument directive or create a maintenance.html file. Here is an example for a very basic maintenance page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Service Temporarily Unavailable</title>
		<h1>Service Temporarily Unavailable</h1>
		<p>The site is currently being updated. Please try again later.<p>
		<address>Web Server at example.com</address>

Once that’s included in your maintenance file, put the file in the root directory of your site. Then visit your site via proxy server to verify that everything is working.

This is the technique I use for my own sites. For some projects I create an elaborate maintenance page with interactive functionality, but for quick site updates the simple HTML file works great.

Note that you can skip serving an actual maintenance file and just send the default server response instead. To do so, delete or comment out the ErrorDocument directive in the .htaccess code above.

You may also want to check out this related post for some more ideas: Allow access to external IPs during development

WordPress Plugins

And just because I care, here are two of the top “maintenance mode” plugins for WordPress:

1 Minute Quickie

Update (2013/08/22): Here is a much simpler “on-the-fly” snippet that I use when doing just the quickest updates on sites:

<IfModule mod_rewrite.c>
	RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.00
	RewriteCond %{REMOTE_ADDR} !^987\.654\.321\.00
	RewriteRule .* - [R=503,L]

Peace out.

Further reading

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress