Take a site temporarily offline

This is a summary for me of http://25yearsofprogramming.com/blog/20070704.htm. That article contains additional information that may be of use to you, and I would strongly recommend reading it.

To temporarily remove a web site (while updates are being done, to recover from a successful attack on your site, etc...), the simplest solution is to use .htaccess to rewrite all incoming requests, pointing them to some page that explains what is going on and telling the visitor when to expect access again.

The rules in the attached .htaccess enable you to also allow one (or more) specific IP addresses to view the site correctly. This allows you to work on the site, reviewing your modifications, while showing other visitors the error message.

NOTE: this requires mod_rewrite to be enabled on your server. If it is not, add it to the server, enable it in Apache, and test.

  1. Copy the file maintenance.php to your server, renaming it if you like. Review the file, modify as necessary.
    1. In the line header('Retry-After: 172800');, the number 172800 is the number of seconds you are telling robots (spiders, crawlers, whatever) to wait before re-indexing your site. 172800 is 48 hours, or two days. Make this slightly longer than your worst case estimate on how long it will take you to repair the site to ensure the spiders do not come back too soon (and downgrade your site in search results). Calculate the number of seconds by multiplying the number of hours by 3600, or 60 * 60 (60 seconds in a minute, sixty minutes in an hour).
    2. Everything between the <html> and </html> is a pure HTML file, and you can edit it as suits your needs. At the very least, put the name of your web site where it says <p><b>Name of website</b></p>.
    3. Open the URL Remember, you can view this file by using the url http://myserver/maintenance.php (replacing myserver with your site URL) and view the file. This is what visitors will see during the outage.
  2. Copy the file htaccess to the root directory of your site, and rename it to .htaccess (initial period required). Edit as appropriate:
    1. Edit the line RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444$ and put your IP address in its place, but do not uncomment it yet. This will allow you to access your site normally. This is a Regular Expression, and the initial caret (^) and trailing dollar sign ($) are required. Also, the back slashes (\) before each period are required.
    2. IF you changed the name of the target web page from maintenance.php, change it in both places in the htaccess file. In the first occurence, the bang (!), caret, and dollar signs are required, as is a slash before any period. Again, it is a regular expression. Note the leading slash (/) which states "in the document root of my web site".
  3. Test the site. You should see the contents of maintenance.php.
  4. Edit the .htaccess file again, and uncomment the line #RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444$ by removing the pound sign (#) from it. You should now be able to access your site, but everyone else will see the contents of maintenance.php.
  5. Edit your site as appropriate.
  6. To remove the redirect, simply rename the .htaccess file to something else, such as htaccess.disable_site, or simply remove the file.

Attached files: htaccess, maintenance.php

Last update:
2012-02-03 07:49
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.