.htaccess “Down For Maintenance” Page Redirect

I recently needed to move one website from a shared web host to our internal server. After some discussion, we decided to simply add a "Site Down For Maintenance" page to the site to prevent users from submitting orders during the hosting change. Using the following .htaccess code snippet, we were able to send all users to a maintenance.html page no matter which page they requested:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

Once we posted the maintenance.html page and .htaccess code on both the old hosting environment AND new hosting environment, we switched the DNS settings. Before making the switch, we had ported the website's code to a "utility" domain and made adjustments so that the website would function well in the new hosting environment. Now that the DNS had been changed, we wanted to make sure that the website would function well on the new domain within the new hosting environment. Unfortunately the code above blocks EVERYONE from accessing any file besides the maintenance.html file. Fortunately my gifted IT team had the answer:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

The above code sends all users to maintenance.html EXCEPT those with the specified IP, which just so happened to be us. We got to test the website while others were locked out. When we were satisfied with the website, we removed the .htaccess code and the site was back up immediately!


Comments

  1. Eric Wendelin

    Nice David! I think I’ll be using this in a month or two…

  2. Mark

    You might want to add the HTTP 501 code so the search engines know its temporarily down.

  3. Mark

    Not sure if my other comment went through. The error code should be 503

  4. david

    @Mark: Good idea. In this case, we simply took the site down for a few hours so a 503 probably wasn’t necessary. Great suggestion for longer cases though.

  5. Phil Thompson

    This is really important so thanks David for documenting it for people. Of course, if you change servers for an ecommerce site (or any type of site for that matter) without taking this approach (or similar) orders can get added via the old server environment so it’s really important

  6. Beth

    “Using the following .htaccess code snippet, we were able to send all users to a “maintenance.html” page no matter which page they requested”

    Anybody know how to do this 0n an IIS server? Right now our server guy just swaps out the default.asp page to a “down for maintenance” page. Problem is, this is only effective for those going directly to the homepage which is only approx. 15% of our users. Most come in from the search engines to product detail pages or other areas. How can be make the maintenance page show no matter which page they request?

  7. Hix Myrick

    This worked great for me. Thank you David. One thing I found was that when including an image on the “maintenance.html” page, it was blocked, too. So I put the image on another web server and linked to it there. Worked as expected.

  8. Etienne

    Wow, this will be useful! Thanks

  9. Steve

    Perfect. Thanks, David.

  10. AR

    Thank you! This was really helpful. How do I add more than one IP address?

  11. Aaron

    sorry if this sounds pointless or anything but on my maintenance.html page it dosn’t show the pictures i added to the .css is there a reason why?

  12. Cody

    Greetings Aaron,

    The reason for this is because this htaccess script is blocking the path to your images. So to get around this, add the following to it:

    RewriteCond %{REQUEST_URI} !^/images/.*$

    RewriteCond %{REQUEST_URI} !^/css/.*$

    Basically, all your images under http://domain.com/images/ and css under http://domain.com/css/ should now work.

    Best Regards,
    cody

  13. Andres Salgado

    Cody I tried the method you describe in order to be able to show images. It did not work, any thoughts….

  14. Cody

    Andres,

    I’m not sure what tell you since I don’t know what your htaccess script looks like. If you wouldn’t mind posting it, I can take a look.

    Best Regards,
    cody

  15. AR

    Would this work on a windows server as well?

  16. David Walsh

    @AR: No, IIS servers don’t work with the .htaccess system of settings.

  17. AR

    Thank you very much.

  18. softwant

    thank you so much.
    this method is what i try to find out. ^^

    have a good time !!!

  19. chris

    Just found this. Any way to add a date to it so that the search engines know when to come back? I am looking at a week shut down to do some deep maintenance on some site problems.

  20. Ken Dawes

    Cool!
    I think that this is what I am looking for.

    Could it be used (how would I make it work..) this way?

    I have a client with an existing URL and website… I am about to develop a new website for her with a different URL on a different host. While I am working on the new site, she would like to be able to give people her new URL but have them redirected to the old site.

    Will it do the trick?
    Thanks!

  21. Andy

    Thanks for that code.

    I was struggling to even get started, but as usual the internet has the answers. I started with the code to redirect everything, then found your code to allow certain IPs to address it, and then to keep the format, found this line

    RewriteRule \.(css|jpe?g|png|gif)$ – [L]

    all hunky dory now for maintenance purposes. cheers

  22. Mike T.

    I couldn’t get the darn images to show up in my .htaccess maintenance page. Thanks for the help. It totally works now.

  23. prateek

    simply awesome !! … it worked !!

  24. piyush

    is there any way to redirect

    abc.com /forum/topic title.php
    to
    pqr.com /forum/topic title.php ??

  25. Paul Mason

    Is there anyway to make the redirect url relative rather than absolute?

    Also has anyone got good tutorial source for htaccess?

  26. Matt

    If you want to gain access to any folder while in maintenance mode, just add this code before the code above:

    Change the name of “myfolder” to any folder you want to access.

    RewriteRule ^(myfolder)($|/) – [L]

  27. Randolph

    Hi, How do I redirect multiple pages to the maintenance page and not just the index page?

    Example:
    mydomain.com >>> mydomain.com/maintenance.php
    mydomain.com/member.php to mydomain.com/maintenance.php

    I need to add 2 pages to redirect to maintenance. Or is it possible to just redirect all of my site’s pages to the maintenance page?

    Hope you could help me.

    Thanks

  28. Edward

    @Randolph The example code already handles every page of the website, “Using the following .htaccess code snippet, we were able to send all users to a maintenance.html page no matter which page they requested”. Give it a try.

    @David Do you know how to re-direct your maintenance page back to your index page when maintenance is over? I’d like to do that with my .htaccess file so I can leave my maintenance page online for when it’s next needed, but prevent people seeing it until I’m carrying out maintenance. Thanks in advance, great post!

  29. Max

    Thanks for your straightforward tip. My Google search turned up many pages with much more complicated methods (a silly DNS hack, restarting the whole Apache server with a different config., etc.).

    Your method is definitely the easiest and most flexible. Thank you!


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: