Temporarily take your site down

October 30th, 2011 | by |

Oct
30


Temporarily unavailableTemporarily taking our sites down is something we all are trying to avoid, but there are times when it’s necessary.

Maybe you’ve been hacked, maybe you need to move a backend server, or maybe you just need to take the site down for some maintenance work that can’t be done whilst online.

Whatever the reason is, even if it’s just temporarily it’s essential that it’s taken down properly.

Do not simply leave an infected or erroneous page, it will send a 200 code to the client, basically saying all is good, resulting in GoogleBot etc indexing your erroneous content.

Do not just put up a page temporarily with a message to your visitors that you will be right back, for the same reason as above. Visiting bots will think everything is cool and index your temporary page. Which is exactly what happend to disney.com a little while ago. They had their temporary page show up first thing on Google for a long time.

Do not just deny access temporarily ( 403 code ), it will also cause problems. Visitors including bots have no way of knowing if this is for 5 minutes or permanently, which might get your pages deindexed.

Do make sure to make both your visitors and visiting bots aware that the site is just temporarily down.

There is different ways of doing this, but they all include sending the client a “503 service unavailable” code.

Sending the client a ”503 service unavailable” code will tell visitor that the site is down temporarily and not permanently, GoogleBot and most other bots respects this and will come back later.

Here’s a few examples on how to take your site down temporarily using php and apache or nginx.

 

Apache .htaccess

ErrorDocument 503 "We are temporarily down due to... Back at..."
RewriteEngine On
# Put your own ip below to be able to allow yourself access
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123$
RewriteRule .* - [R=503,L]

This works fine and allows you to show a message to your visitors about why the site is temporarily down and when it’s expected to be back up.

However, spiders and bots won’t understand that. They will know that the site is temporarily down, but they will not know when it’s expected to be back up.

Nginx

location / {
  if (-f $document_root/maintenance.html) {
    return 503;
  }
  ..... rest of your normal config here
}

error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /maintenance.html break;
}

 

To put your site into maintenance mode simply put a message into the maintenance.html file, and remove it once finished.

This is similar to the apache version above, it works and it shows a message to the user letting them know the site is temporarily down, but it doesn’t let the clients/spiders know when the site is expected to be back up.

 

 

PHP

<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
print "We are having some issues, check back in a while";

 

 

In the code above we first set the header with the 503 Service Temporarily Unavailable code, but then we also add the “Retry-After” part of the header, telling the client/bot when it should retry the page again.

In our case we tell the client to retry after 3600 seconds, ie 1 hour.

To get this to work with apache we can use the following in our .htaccess file ( remember to replace /var/ww with the actual path to your file ).

RewriteEngine On
RewriteRule .* /var/www/maintenance.php

 

This will redirect all requests to your maintenance page and at the same time telling the client to revisit after 1 hour.

 

Nginx version below

 

if (-f $document_root/maintenance.php) {
  return 503;
}

error_page 503 @maintenance;
location @maintenance {
  fastcgi_pass   localhost:9000;
  fastcgi_param  SCRIPT_FILENAME $document_root/maintenance.php;
  fastcgi_param  PATH_INFO $fastcgi_script_name;
  include /etc/nginx/fastcgi_params;
}

And just like the apache version above this will also redirect all requests to your maintenance page and at the same time telling the client to revisit after 1 hour.

 

Google them selves recommend this way of taking a site down temporarily.

So the next time you’re taking your site down temporarily, at least do it correctly :)

 

 

 

No Comments »