Apache config can be used to redirect all traffic when doing development / maintenance on your website and you want to hide it from public purview.


Sometimes your website is under development / maintenance, so it can behave wonky or maybe you’re planning a surprise.  There’s various ways to go about dealing with it, one is apache config to redirect all traffic elsewhere, like a notification page.


RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteCond %{DOCUMENT_ROOT}/maintenance.enable -f
RewriteCond %{REQUEST_URI} !^/KV_503_maintenance/
RewriteRule ^.*$ /KV_maintenance/index.html [R=503,L]
ErrorDocument 503 /KV_503_maintenance/index.html

This needs to be placed inside your vhost. The 443 SSL vhost alone is fine, if you do force https.

The 2nd line is optional, it basically whitelists an IP address, you could put the WAN IP address of your internet connection here.  Anyone accessing the site from this IP address will NOT be redirected.  Make sure to escape the dots as in the example.

The 3rd line says it will only apply the redirect if it encounters “maintenance.enable” file in the document root of the vhost.  This file could contain anything or nothing at all, it’s merely it’s existence.  This condition allows for easy turning on / of maintenance mode.

The 4th line excludes anything in the URI starting with those directories, which is important because else it won’t be able to access the redirected landing page, but choke on an infinite loop.  The example uses directory rather than file, because like this you can still have images and css in your redirected landing page (without all of them requiring to be explicitly allowed.

It finishes with the actual redirect and by also stating that as ErrorDocument, with a suitable http status code 503.  Service Unavailable implies temporary, which is what we want for maintenance, because we want browsers to check at the original URI again upon next visit, as we might have finished maintenance.


RewriteRule ^.*$ https://common.example.com/KV_maintenance/index.html [R=503,L]
ErrorDocument 503 https://common.example.com/KV_503_maintenance/index.html

If you simply want 1 maintenance page for all your vhosts.  In this case line 4 isn’t needed, because the resource is on a different vhost (subdomain).