Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wataruoguchi/649963c3ee18640e0105 to your computer and use it in GitHub Desktop.
Save wataruoguchi/649963c3ee18640e0105 to your computer and use it in GitHub Desktop.
How to make your website to maintenance mode - Apache

Apache

How to make your website to maintenance mode

  1. Put maintenance.html into your root directory

  2. Open .htaccess file

  3. Insert this block in the first line of the file

     ErrorDocument 503 /maintenance.html
     <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{REQUEST_URI} !=/maintenance.html
     RewriteCond %{REMOTE_ADDR} !=127.0.0.1
     RewriteRule ^.*$ - [R=503,L]
     </IfModule>
    
  4. Replace 127.0.0.1 to your ip address

  5. Done! Users except you will be redirected to maintenance.html

So what does it exactly do?

ErrorDocument 503 /maintenance.html is when user gets error 503, redirect the page to maintenance.html

RewriteRule ^.*$ - [R=503,L] does rewrite any url without replacing the url and access with 503

@AlexD1979
Copy link

AlexD1979 commented May 6, 2019

Hello, nice function. I try it for my use-case, but it does not work as expected.
I add some line of codes to exlude the pictures fro the maintenance page and also exclude the healthcheck file for the loadbalancer check. Theres requests should not be redirected to the maintenance page.

` # Redirect all request to a 503 return code when in maintenance mode

    ErrorDocument 503 /maintenance/index.html
    RewriteEngine on
    RewriteCond  /apps/myApp/maintenance/ALL -f [OR]
    RewriteCond  /apps/myApp/maintenance/%{SERVER_NAME} -f
    RewriteCond %{REQUEST_URI} !=/maintenance/index.html
    RewriteCond %{REQUEST_URI} !=programdata/images/header.jpg
    RewriteCond %{REQUEST_URI} !=programdata/images/footer.jpg
    RewriteCond %{REQUEST_URI} !=programdata/images/logo.png
    RewriteCond %{REQUEST_URI} !=probe/healthcheck.cfm
    RewriteRule ^ - [R=503,L]
    # Redirect away from the maintenance page if not in maintenance mode
    RewriteCond  /apps/myApp/maintenance/ALL !-f
    RewriteCond  /apps/myApp/maintenance/%{SERVER_NAME} !-f
    RewriteRule ^/maintenance/index.html$ / [R,L]`

It works, if I access the webpage in root, means http://localhost then I will redirected to the maintenance page. But If I call the page in subfolder, http://localhost/admin I see the text of my maintenance page, but the images are not displayed.

@Jazzgeir
Copy link

This was very useful! I am using this in my web servers. This solution requires no extra load on file system and does not require a restart of webserver to be activated. Good work!

One thing, though: If you have subfolders with their own .htaccess files with rewrite rules, you'll need this line in the root directory .htaccess to override the subfolder .htaccess rewrite rules: RewriteOptions InheritDownBefore

I spent some time before sorting this out, so I thought it'd be nice to get back to you with this info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment