Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

.htaccess “Down For Maintenance” Page Redirect

23 Responses »

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!

Discussion

  1. May 22, 2008 @ 10:57 am

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

  2. May 22, 2008 @ 3:57 pm

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

  3. May 22, 2008 @ 3:58 pm
  4. May 22, 2008 @ 4:01 pm

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

  5. May 22, 2008 @ 9:34 pm

    @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.

  6. May 23, 2008 @ 10:16 am

    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

  7. beth
    June 11, 2008 @ 2:31 pm

    “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?

  8. March 10, 2009 @ 12:30 pm

    Oh dear, I’ve been looking how to do this and even my hosting company couldn’t give me what I needed. Cheers. Fast becoming one of my favourite site. No nonsense, everyday solutions, for everyday web designers!!

    Cheers.

  9. June 27, 2009 @ 9:56 am

    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.

  10. July 21, 2009 @ 3:46 pm

    Wow, this will be useful! Thanks

  11. steve
    August 13, 2009 @ 9:11 am

    Perfect. Thanks, David.

  12. August 18, 2009 @ 2:19 pm

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

  13. aaron
    August 27, 2009 @ 6:12 pm

    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?

  14. August 31, 2009 @ 8:43 pm

    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

  15. andres salgado
    September 10, 2009 @ 8:21 pm

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

  16. September 10, 2009 @ 9:07 pm

    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

  17. October 4, 2009 @ 11:51 pm

    Would this work on a windows server as well?

  18. October 5, 2009 @ 7:33 am

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

  19. October 6, 2009 @ 1:29 pm

    Thank you very much.

  20. softwant
    October 21, 2009 @ 9:59 am

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

    have a good time !!!

  21. chris
    November 18, 2009 @ 11:24 pm

    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.

  22. ken dawes
    December 23, 2009 @ 12:30 pm

    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!

  23. July 12, 2010 @ 4:38 pm

    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

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!