How to create a maintenance mode with htaccess

As you can see my blog has been enjoying a new theme for almost 3 days now. As you can see this is not a run off the mill theme: It features some very custom stuff like the portfolio and the sliders. So when I was updating my blog, I knew I needed some time to get everything up and running. As I didn’t want everyone to see how clumsy I am, I decided to actually block the whole site for everybody except me. Most people call this maintenance mode.
There’s various WordPress plugins out there that handle maintenance mode for you, but they are all WordPress-based. That means: If you break WordPress for whatever reason, everybody will be able to see whatever you’re doing. It’s a bit like walking around naked while being invisible: You’re pretty sure nobody can see you, but what if you suddenly turn visible? (Yea, strange comparison but I’m pretty sure at least one of you guys out there gets the metaphor).
So what did I do?
I created a .htaccess based maintenance mode. The cool thing about this is that you can do this without WordPress at all. So you can replace your old WordPress blog with a new one, some other blogging software or a custom site. And nobody will be able to see you mess up.
How does it work?
If you don’t already have an .htaccess file in the root of your domain, create one and add the following code. If you already have an .htaccess file, add the following code in front of everything that might be in there (“Why” you ask? Before evaluating the request, make sure that whoever is trying to access the website, is in fact allowed to do so).
RewriteEngine On
# Add all the IP addresses of people that are helping in development
# and need to be able to get past the maintenance mode.
# One might call this the 'allow people list'
RewriteCond %{REMOTE_HOST} !^83\.101\.79\.62
RewriteCond %{REMOTE_HOST} !^91\.181\.207\.191
# Make sure the <em>maintenance mode</em> only applies to this domain
# Example: I am hosting different sites on my server
# which could be affected by these rules.
RewriteCond %{HTTP_HOST} ^nocreativity.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nocreativity.com$
# This is the 'ignore file list'. It allows access to all
# files that are needed to display the <em>maintenance mode</em> page.
# Example: pages, css files, js files, images, anything.
# IMPORTANT: If you don't do this properly, visitors will end up with
# endless redirect loops in their browser.
RewriteCond %{REQUEST_URI} !/offline\.htm$
RewriteCond %{REQUEST_URI} !/css\/style\.css$
RewriteCond %{REQUEST_URI} !/images\/logo\.png$
# Rewrite whatever request is coming in to the <em>maintenance mode</em> page
# The R=302 tells browsers (and search engines) that this
# redirect is only temporarily.
# L stops any other rules below this from executing whenever somebody is redirected.
RewriteRule \.*$ /offline.htm [R=302,L]What this does is keep everybody out from your domain, except the development people. You can add those by just adding their IP addresses to the allow people list. Everybody else will get redirected to offline.htm (which includes a css file and an image). In order to let people access the maintenance mode page on the domain, you need to add those files to the ‘ignore file list’. If you don’t, visitors won’t be able to access these files and will get endlessly redirected.
This worked fine for me: I was able to test and develop everything. When I was ready to let some friends in to check the site for errors I just had to add them to the allow people list and they were in… until I removed them from the allow people list
I like this kind of control when working on stuff like this. Once you’re ready to go live, just remove those rules from the .htaccess file (or remove the file altogether) and you’re done!
I’m most definitely not a .htaccess guru. I found this to be very helpful when I started updating the site and I decided to share this with you. Let me know if you have better ways of setting up a maintenance mode.























There are no comments yet, add one below.