.htaccess “Down For Maintenance” Page Redirect

By  on  

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!

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

Discussion

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

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

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

  4. @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. 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. 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. Wow, this will be useful! Thanks

  9. Steve

    Perfect. Thanks, David.

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

    • CBloss

      Hey Cody!

      Thanks for the additional tip! I was having trouble with that! ^_^

  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. 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. Would this work on a windows server as well?

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

  17. 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. 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. I couldn’t get the darn images to show up in my .htaccess maintenance page. Thanks for the help. It totally works now.

  23. simply awesome !! … it worked !!

  24. is there any way to redirect

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

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

    Also has anyone got good tutorial source for htaccess?

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

  30. @Edward
    you can do it by this code. But you need to comment this code when you want to redirect the user to maintenance page.

    RewriteRule (maintenance.php)$ index.php [R=302,L]

  31. Nice and neat, thanks… ^^

  32. Nice and neat, tanks…. ^^

  33. This one worked. I’ve tried many htaccess tricks but this one is the one working.
    I even added some more IPs by simply copy paste the line RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
    few more times.

  34. Michael

    Really terrific, thanks a lot!

  35. awesome! works perfectly.

  36. Jesse

    Hey ive tried this copied your setup then changed the required text but i keep getting a 500 internal error. Any ideas why this could be happening?

  37. Jesse

    Sorted 500 error! mod_rewrite was not working properly. Heres how to fix if anyone having the same issue

    rm /etc/apache2/mods-enabled/rewrite.load
    a2enmod rewrite
    service apache2 restart

  38. Sarah Bercier

    so i added the code to my .htacess page to my site louisianajam.com and now i can’t access it to make the changes I need. Can you help me figure out what i did wrong?

  39. Chris

    The issue with this solution is the fact that all pages are redirected to /maintenance.html.

    A better solution is to keep the URL to which users browse and replace the content of the page with the maintenance.html content and respond with HTTP 503.

    A better solution is documented on http://www.shellhacks.com/en/Redirect-Site-to-Maintenance-Page-using-Apache-and-HTAccess

  40. Dan

    Thanks for this information.

    Will the ‘Public’ IP # work? Or does it have to be the private IP # for it to work?

  41. Skip

    What if, instead of maintenance.html I wanted to redirect to a directory, likst /SiteDown?

  42. Loren

    Just wanted to say, “thank you!”

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!