How to put Magento in maintenance mode with IP filters (the easy way)?

This is probably the most useful quick tip I can think of that I use all the time. Putting Magento in maintenance mode is an essential part of every site launch and it is used in lots of other situations. Although there are many different ways to do this I found that using a few simple lines of code in the root htaccess file is, by far, the easiest solution I could think of. On top of that, this solution enables you to put all the files needed for the maintenance page (html, images, etc.) into one folder. One more advantage of this method is that if you forget to add your IP address to the filter (whitelist) you can do that at any time through FTP so you will not be locked out of your own site 😉

Before we start I recommend you backup your root htaccess file so you can easily reverse all the changes if anything goes wrong.

First you will need to create a folder called “maintenance” in the root of your Magento site. Put the maintenance page (for example static html file) and all the require resources (like images and css) into the folder you just created. Maintenance page can be anything you like as long as all the necessary files are also located in the “maintenance” folder. Be sure to use relative paths when adding resources (images, css, etc.) to your maintenance page. They should be relative to maintenance folder you just created. You can design the maintenance page anyway you like and add all the content and features you need.

Once you are happy with your maintenance page you need to add a few lines of code into the root htacess file. Add the following code below rewrite engine declaration (“RewriteEngine on”). Make sure you have the rewrite engine turned on and working before adding the code.

############################################
## 503 Maintenance mode
RewriteCond %{REQUEST_URI} !^/maintenance/
RewriteCond %{REMOTE_HOST} !^YOUR.IP.ADDRESS.HERE$
RewriteCond %{REMOTE_HOST} !^127.0.0.1$
 
RewriteRule .* index.php [R=503,L]
ErrorDocument 503 /maintenance/maintenance.html

Add you IP address in the format specified in line four. You can add as many IP addresses as you need and they will all be able to access the site while it is in maintenance mode. All other IP addresses will be redirected to the maintenance page. Leave the 127.0.0.1 as is or it won’t work correctly.

Note that we used 503 redirect which prevents search engines from removing your site from their index.

Save and upload your htaccess file and everything should work fine. Please try this on your local machine or development/staging server before moving it to your live site. I am not responsible if you break your site while trying to make this work! On the other hand if you made the backup like I told you at the beginning you should be OK 🙂 If you experience any problems just replace the htaccess file you worked on with your backup and you’ll be fine (no need to delete the maintenance folder).

That’s it. I hope it will work for you. In case you’ll need some extra help along the way, we’re here for you!