Redirecting visitors to a maintenance page or other temporary page is an essential tool to have in your tool belt. Using HTAccess, redirecting visitors to a temporary maintenance page is simple and effective. All you need to redirect your visitors is the following code placed in your site’s root HTAccess:

That is the official copy-&-paste goodness right there. Just grab, gulp and go. Of course, there are a few more details for those who may be unfamiliar with the process. Let’s look at all the gory details..

Redirecting Traffic with HTAccess

To redirect your visitors to a maintenance page, place the previous code into an HTAccess file located in your site’s root directory. This will ensure that all pages and resources contained within your domain will be redirected for visitors. Thus, if you would like to redirect only requests for a specific subdirectory within your domain, the .htaccess file containing these rules would be placed in that directory (instead of root).

Now that the HTAccess is in place, you’ll need to create and upload your maintenance page, named “maintenance.html”, to the root directory of your site. This file can be just about anything, and does not need to be written in HTML. You can use, say, PHP to make it all dynamic, but remember to change the two instances of the file name in the HTAccess code to match the actual name of your file.

Code Explanation

The first line is merely a comment to explain the purpose of the code. It is not processed by the server.

The second line enables Apache’s rewrite engine, mod_rewrite. Depending on your server setup, this line may be unnecessary.

The third line checks to see if the request is coming from your computer. If it is, then the redirect does not happen. For this to work, you need to change the numbers in this line to match your own IP address.

The fourth line prevents an infinite-loop scenario by testing the request against the name of your maintenance page. Obviously, we don’t want to redirect requests for the page to which we are redirecting.

The fifth and final line contains the action. It basically redirects all requests that meet both of the previous rewrite conditions to the specified maintenance page. Apache doesn’t allow us to use 500-level response codes, so we are stuck with the next best thing, a 302 – temporary – response.

Update: Multiple IP Addresses

It was asked in the comments how this might work when you want to allow multiple IP addresses. There are basically two ways. First, you can add more RewriteConds to the previous code, like so:

With this method, edit each IP address to match your own, and add/remove as many IPs as needed. The second method is equally effective, and looks like this:

<Limit GET POST PUT>
Order Deny,Allow
Deny from all
Allow from 123.456.789.000
Allow from 123.456.789.000
</Limit>
ErrorDocument 403 /maintenance.html
<Files maintenance.html>
Order Allow,Deny
Allow from all
</Files>

This method is a bit simpler, but not as good for SEO should the search engines visit while in maintenance mode. The first method sends a 302 - Moved temporarily status code, while the second sends a less accurate 403 - Forbidden status code. Even so, should you go with method #2, edit the IPs to those of your own, adding or removing new lines as needed for site access.

It’s like a Pandora’s Box..

It’s difficult to keep posts short and sweet when working with HTAccess techniques. There is just so much that you can do with it. For example, we can do htaccess password-protection, allow access to multiple visitors, request specific redirects, and much more. But I refrained from complicating things in an effort to keep this post focused and on-topic. Nonetheless, there is always room for improvement, so if you see something that could make this simpleHTAccess-redirect technique even better, then please share via the comments. Thanks!

When I paste this into my .htaccess file inside of my WordPress sub-directory all seems well, except I can not longer view posts. Whenever I try to access a post during “maintenance mode”, I receive a 404 File Not Found error. When I remove the code suggested above, everything works again. Is this normal behavior?

Although the IP address is nice for me to view the site, I would prefer to have a subdomain instead but I’m not having any luck.

Is it possible to use this redirect for the main domain.com & www.domain.com and then use a subdomain e.g. dev.domain.com to show the site as normal?

I tried adding RewriteCond %{HTTP_HOST} !^dev\.domain\.com$ [NC]
as an extra condition which bypasses the maintenance mode page, however, instead of loading the home page of the site it’s loading my servers default index page.

Oh well, I just thought it would be more flexible to have a subdomain as a bypass rather than the ip address… clearly I am still learning the ins and outs of htaccess.

Hi Randolph, I’m pretty sure that’s what the code presented in the article does for you – redirects all requests (except for images and the maintenance page itself) to the maintenance page. Check the article for more information.