Redirect Site to Maintenance Page using Apache and HTAccess

In this article you will find how to redirect all traffic and all visitors of your site to a maintenance page during site updates.

I’ll show how to create a maintenance page, how to put the site into a maintenance mode and how to bring it back online without restarting Apache.

You’ll learn how to redirect Apache Vhost’s traffic to a maintenance page using either VirtualHost configuration file or using .HTAccess file.

Cool Tip: Deny access from specific IPs via .htaccess! Read more →

Create a Maintenance Page

First of all, you have to create 2 files inside a site’s document root:

File Purpose
maintenance.html A file with the HTML code of the maintenance page
maintenance.enable An empty file that will activate the maintenance mode

[maintenance.html]

Create a maintenance.html page that will tell the visitors that your website is down for maintenance.

As a template, you can use the following simple HTML code of the maintenance page (demo):

<!DOCTYPE html>
<html>
    <head>
        <title>Down For Maintenance</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style>
        h1 { font-size: 50px; }
        body { text-align:center; font: 20px Helvetica, sans-serif; color: #333; }
        </style>
    </head>
    <body>
        <h1>Down For Maintenance</h1>
	<p>Sorry for the inconvenience, but we’re performing a maintenance at the moment.</p>
	<p>We’ll be back online shortly!</p>
    </body>
</html>

[maintenance.enable]

Create an empty file, named maintenance.enable, inside the site’s document root.

We’ll use it to turn the maintenance mode ON and OFF, without Apache restart.

We’ll setup Apache to activate maintenance mode and redirect all site’s visitors to maintenance page, if the file maintenance.enable is found inside the site’s document root. Otherwise the maintenance mode will be disabled.

So, if we need to TURN ON site’s redirection during maintenance, we create an empty maintenance.enable file.

And when we need to TURN OFF redirection after maintenance, we delete maintenance.enable file.

Redirect Apache Vhost to Maintenance Page

Add the following Rewrite Rules, to the configuration file of Apache Virtual Host to redirect all your site’s visitors to maintenance page:

This snippet ought to be added before any other Rewrite Rules or Conditions.

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
Header Set Cache-Control "max-age=0, no-store"

Reload Apache, to apply new settings:

# apachectl graceful

From now we can TURN ON and TURN OFF the maintenance mode, with maintenance.enable file, without Apache restart/reload.

Redirect to Maintenance Page Using HTAccess

Place the following code in your site’s root .htaccess file to redirect all visitors to the maintenance page:

This snippet ought to be added before any other Rewrite Rules or Conditions.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html
Header Set Cache-Control "max-age=0, no-store"
</IfModule>

Explanation

1. Turn on the Rewrite Engine.

RewriteEngine On

2. (optional) Don’t match your IP address. Use this directive to prevent redirection to maintenance.html for traffic from listed IP address.

RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000

You can set several IP addresses line by line, as follows:

RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.1
RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.100
RewriteCond %{REMOTE_ADDR} !^172\.16\.10\.15

3. Make sure the maintenance.html page exists.

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f

4. Check for the maintenance.enable file (this is how you turn the maintenance page on and off).

RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f

5. Don’t apply the rule when serving the maintenance page (avoids circular rewrites).

RewriteCond %{SCRIPT_FILENAME} !maintenance.html

6. The 503 redirect to maintenance page itself.

RewriteRule ^.*$ /maintenance.html [R=503,L]
ErrorDocument 503 /maintenance.html

503 Service Unavailable – HTTP status code, that means that the server is currently unavailable (because it is overloaded or down for maintenance).

7. Avoid caching.

Header Set Cache-Control "max-age=0, no-store"
Was it useful? Share this post with the world!

22 Replies to “Redirect Site to Maintenance Page using Apache and HTAccess”

  1. Thanks for the guide. I altered your solution slightly because I didn’t want to disable caching for my whole site at all times but just when it was in maintenance mode. I did this by setting my maintenance page to a PHP script with the following at the top…

    1. You are welcome.

      1. Is there anyway we can redirect to html page when Apache itself down?

    2. Hi Ben,

      Can you please share the change you made,I want to ensure that caching is disabled only for the maintenance page and not other jsps stc

  2. How we can achieve to disable cache only when the website is in maintenance

    1. It already works this way. According to the above code, cache will be set conditionally – only when maintenance mode is enabled.

      1. Why are you saying that? It seems that Header doesn’t depend on RewriteCond, neither as ErrorDocument. These are a pretty major drawbacks in my case.

  3. Jason Collum says: Reply

    Thank you for the steps.
    I do have a scenario I am curious how you would handle it.
    The scenario is to present the maintenance page early (full or embedded) or temporarily and the allow user to continue.

  4. Thank you for this. I have a script that I put in a controller that I need to run during maintenance mode. Will I be able to successfully hit the endpoint to execute this script while in maintenance mode?

  5. I think using the ErrorDocument for 503 error is much better. This will allow for user’s URL to simply “passthrough” and not redirect them to some maintenance URL. See this answer:

    https://stackoverflow.com/questions/622466/how-to-put-apache-website-to-503-temporary-down#5447534

    1. This is not an ideal approach for public websites where custom content for the maintenance effort needs to be displayed/communicated.

  6. Если на странице используется вызов style.css и другое подключение из файлов – такой вариант не работает. Вам необходимо будет переносить все скрипты и стили внутрь html – что не рекомендуется по W3C

    1. You Can use “Alias” (alias module) to redirect static contents to local path:

      Alias /static-imgs/ “/var/www/img/”

      Then, you have to add the next rewrite condition:
      RewriteCond %{SCRIPT_FILENAME} !/static-imgs/*

      Finally you use the /static-img/ path for static content on your html mainteneace file:

      It Works perfecctly!!

  7. How can you allow favicon.ico to be processed normally – with your setup the browser’s request for favicon.ico is also rewritten to maintenance.html

    I tried added a !favicon.ico as a rewritecond, but it doesn’t seem to work.

  8. Do you know if I can put a wordpress website that has already been suspended by the hosting service in maintenance like you describe?

  9. Hi
    i am not finding .htaccess in my apache setup on windows
    Regards
    Harish

  10. It doesn’t work at my end. I used the .htaccess redirect with PHP website contents. I tried to use the .php extension for the maintenance file, as well as .html but it did not work.

  11. DocumentRoot "/myapp/doc/PAQ" 
    RewriteEngine On
    RewriteCond  /myapp/PLQ1/static/maintenance/maintenance.html -f
    RewriteCond  /myapp/PLQ1/static/maintenance/maintenance.enable -f
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /maintenance.html [R=503,L]
    ErrorDocument 503 /maintenance.html
    Header Set Cache-Control "max-age=0, no-store"

    If we put “maintenance.html ” in different folder / absolute path, not working. How to resolve this issue?

  12. Rajyalakshmi says: Reply

    HI, Can you please tell me how to do in jetty server

  13. It’s such a wonderful solution. Thank you!

  14. Can I use another directory path instead of the document root?
    Say document root is /var/www/html, can i use /home/User/503page/503.htm ?? instead and then point to the directory RewriteCond /home/user/404page/404.html -f . Is that Possible??

Leave a Reply