Maintenance mode via .htaccess

Many times we are asked by our clients how to put the site offline but still be able to work on it. We will show in this article how to set a “maintenance mode”/ “down for maintenance” page.

WordPress users can simply use a plugin. Just search, install and test the one that suits your needs. Look at https://wordpress.org/plugins/search/maintenance+mode/

We discuss how you can ‘set’ maintenance mode via the .htaccess file. Notice that .htaccess file is a hidden file. To see hidden files in cPanel, check these articles:
View hidden files in FileZilla
Show hidden files in cPanel

Site Maintenance Mode

The following code snippet will send all web traffic to the maintenance.html file, excepting the traffic from the specified IP address. Replace 123.123.123.123 with your IP address.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123.123.123.123
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ https://domain.com/maintenance.html [L]
</IfModule>

If you are using images, css/js files in the maintenance.html, put them in the /resources/ directory and use:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123.123.123.123

RewriteCond %{REQUEST_URI} !^/resources/.*$
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule ^(.*)$ https://domain.com/maintenance.html [L]
</IfModule>

Some recommend using a redirect code 307 or 503 in the last rule. We do not recommend such a code, as 503 HTTP means Service Unavailable. The 307 HTTP code is Temporary Redirect.

Resources:
HTTP status codes

Share this post: