Turn your website into maintenance mode – SEO friendly solution using .htaccess file

Here is a simple trick which enables to turn your website into maintenance mode using .htaccess file.

If you want to give an informative message saying “We are under maintenance”, create a custom HTML page containing the message and put it in the website root. For e.g., we create an HTML page called “under-maintenance.html” and place it inside the website root, so that we can access it through the URL http://www.yourdomain.com/under-maintenance.html

Setup status “503″ and header “Retry-After”

Depending on the maintenance duration, we also have to care about the search engines which may visit the website during maintenance. For doing this, we will send an http status code 503, “Service Unavailable”, along with the HTML page containing maintenance information. Also, we will inform search engines, visit the website only after the maintenance is completed.

First, setup an error document for http status 503. Specify your maintenance page as the error document as shown below.

ErrorDocument 503 /under-maintenance.html

Now add a small trick using mod rewrite to flag all http responses from the website using status 503.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/under-maintenance.html$
RewriteRule .* - [R=503,L]

Note: The ‘R’ flag can be used to redirect to a destination page only when the status code specified is ranging from 300 to 399. If it’s outside the range, rewrite engine will skip the destination URL and we just gave a – (dash) to specify the destination URL will not be changed.

We have used here a conditional rewrite where the URI is not equal to “/under-maintenance.html”. This is to avoid looping errors which can occur when the rule is enabled.

Additionally, if you are using images or external style sheets inside your maintenance page, don’t forget to exclude them from being redirected. For e.g., add an additional condition per external resource below the existing condition.

RewriteCond %{REQUEST_URI} !^/styles/maintenance.css$

The effective rule will be, URI is not equal to “/under-maintenance.html” AND “/styles/maintenance.css”.

Now, we have one more thing to do, tell search engines visiting your site during maintenance period, come back only after specified period of time to re-check the site. Add below block of code to your .htaccess file to set a “Retry-After” response header indicating a time period or a fixed time.

To re-visit the site only after 10 hours,

<IfModule mod_headers.c>
Header set Retry-After: 36000
</IfModule>

To re-visit after a fixed time,

<IfModule mod_headers.c>
Header set Retry-After: "Wed, 25 Nov 2009 06:30:00 GMT"
</IfModule>

Note: “headers_module” must be enabled to use the “Header” directive inside your .htaccess file. Make sure a similar line of code is enabled inside your httpd.conf file in apache.

LoadModule headers_module modules/mod_headers.so

At last, the final block of code to be inserted at the end of .htaccess file is:

<IfModule mod_rewrite.c>
 ErrorDocument 503 /under-maintenance.html
 RewriteEngine On
 RewriteCond %{REQUEST_URI} !^/under-maintenance.html$
 RewriteCond %{REQUEST_URI} !^/styles/maintenance.css$
 RewriteRule .* - [R=503,L]
 <IfModule mod_headers.c>
 Header set Retry-After: 36000
 </IfModule>
</IfModule>

One Response to Turn your website into maintenance mode – SEO friendly solution using .htaccess file

  1. Daniel Bruessler says:

    Hello,

    thanks! That’s the best maintenance-mode I can imagine.

    Cheers!
    Daniel

Leave a Reply to Daniel Bruessler Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>