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

How to put Magento in maintenance mode with IP filters © eqphotolog@sxc.hu

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. I haven’t had any problems with it so far though.

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


9 comments

  1. Please disregard previous post. I just learned that I couldn’t see the .htaccess file because it is hidden in cpanel

  2. Where do I find the htaccess file? I can’t find it anywhere…using Magento 1.8…very frustrating…

  3. Hi Srdjan,

    thanks a lot for the quicktip its very usefull and worked fine. Maybe it will be part of the standards in Magento2

    greetz

  4. Hi Srdjan
    Thanks for the tip. It worked really well. Not sure why magento doesn’t have this feature available by default.

    M.

  5. Thanks for the tip, Srdjan!

    Are there any suggestion how to do the same if I’m using Nginx?

  6. I had to make the following changes to make it work for me. I paste it below just in case someone finds it helpful:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_ADDR} !^YOUR\.IP\.ADDRESS\.HERE
    RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif) [NC]
    RewriteRule .* index.php [R=503,L]
    ErrorDocument 503 /maintenance/maintenance.html

    Thanks for your post!
    RS

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.