Enable Maintenance Mode With .htaccess

Sometimes when you are upgrading your blog or website, it’s wise and highly recommended that you block all your visitors and redirect them to a maintenance page. So your visitors know and can come back later. You can redirect your visitors to a maintenance page with redirect 307 (temporary redirect). This redirect 307 won’t harm your server or SEO in search engines. When the Google bot sees this redirect code, it will come back later and index your page again.

To redirect all your visitors except yourself to a maintenance page, you can use .htaccess. See the code below:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^123.456.78.90 #your ip address
RewriteCond %{REQUEST_URI} !^/maintenance.php$ #maintenance page
RewriteRule ^(.*)$ http://www.ivankristianto.com/maintenance.php [R=307,L] #path to maintenance page

Code explanation: It checks the visitor’s IP. If it matches the IP whitelist, do not redirect to the maintenance page. If it doesn’t match, it checks if the request is for the maintenance page — if yes, do not redirect (otherwise it would go into an endless loop). If not, redirect to the maintenance page.

And in your maintenance.php you should add this code:

<?php
ob_start();
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
header('X-Powered-By:');
?>

Code explanation: It tells the browser that your service is temporarily unavailable with a 503 status code. It also tells any search engine bot to come back again in 3600 seconds, or 1 hour.

That’s it — don’t forget to turn it off when you are done with maintenance or upgrading your websites/blogs. To turn it off, just comment the last 3 lines with “#” at the beginning of the line.