|
25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more... |
Home Projects Sitemap Search Blog Forum+Chat About Us Privacy Terms of Use Feedback FAQ Images Services Payments Humor |
How to close your website for maintenance with Apache .htaccessYou should, and almost always can, perform routine maintenance on your site without
closing it down, but people sometimes ask how to take a site offline,
so here Only a dire emergency justifies taking a site offline, but if your site was hacked and contains extremely offensive material or viruses, closing it while you repair it will help protect your visitors, prevent offensive content from getting indexed under your site's name, and can prevent your pages from getting flagged with "This site may harm your computer" in Google search results. If you were hacked, see also Step By Step Repair After A Website Hack. All the methods below close the site to normal HTTP browser and crawler traffic, but you will be able to use your control panel and FTP, and your FTP users will have access, too. Four of the methods are easily customized so that you as the owner are allowed to access the site normally, while everybody else is redirected to the maintenance page or blocked. The time your site is closed should be kept to the absolute minimum required to make it safe. The methods that send HTTP response codes of 503 (the best) or 307 (an acceptable alternative) are recommended by Google. CautionsAll the methods involve manually editing your Apache .htaccess file, so you need to know how to do that (Method #4, below, has the details). Before making any changes to .htaccess, save a copy of the original so you can revert to it if necessary. In addition, modifying .htaccess manually is least likely to cause side-effect problems under the following conditions:
If you do use the FrontPage Extensions or cPanel Hotlink Protection, the relatively simple procedures below get more complicated and must be done with great care. Both programs make automated modifications to .htaccess, and each of them "thinks" it owns that file. The two programs can interfere with each other, and your manual edits to .htaccess can make either or both of them think the file is corrupted, for reasons described in a previous article. Reverting to the original .htaccess should be sufficient to restore Hotlink Protection to the way it was, but that might or might not completely restore the Extensions. It might be necessary to uninstall and reinstall them, which can have complications of its own.
Try to avoid FrontPage Extensions conflictsBefore editing .htaccess manually, you can use FrontPage the usual way to upload any new files you'll need (maintenance.php, for example, as described later). After the needed files are in place, close FrontPage and do not use it to connect to your site in any way whatever while you modify and use your new .htaccess. Don't connect with FrontPage again until you've put back the original .htaccess. Try to avoid cPanel Hotlink Protection conflictsTo reduce the chances of corrupting .htaccess:
1) Simplest, almost-best method: send plain text status code 503 - Service UnavailableThis method, because of its simplicity, is easy to install and therefore preferable for most webmasters, especially ones in a hurry. It sends an HTTP response code of "503 - Service Unavailable" which tells search engines that this is a temporary problem. This method only has two drawbacks, both minor:
Put the following code lines in your .htaccess file and customize the highlighted text as appropriate: ErrorDocument 503 "Our website is temporarily
closed for maintenance. It should reopen by..." The ErrorDocument line specifies that the error document for a 503 response will be the one line of plain text inside the quotation marks. The RewriteCond line says that if the request did not come from the given IP address (yours), the rule in the following line will be applied. That is, if you are making the request, the rule won't be applied, and the site will function normally for you. The RewriteRule line says that no matter what page was requested, send a 503 response along with the default "error document" specified a few lines earlier. 2) Best method: Rewrite to return status code 503 - Service UnavailableThis method rewrites the incoming request to a PHP file that returns the 503 response code and tells search engine crawlers how long to wait before trying again, which is the main advantage of using this method. The text on the page can be formatted any way you want, to inform human visitors what the problem is. Procedure
Example code for maintenance.php or maintenance.htmlHere is a maintenance.php you can use for method #2 above. The first line that says <?php must be at the very top of the file, with nothing, not even a blank line, above it. Retry-After tells robots how long to wait (in seconds) before trying again. It is set here to 48 hours to allow for cleaning up a badly damaged website. You can also use this text as maintenance.html for method #3 below. Just leave out the top 5 lines in yellow (the PHP code). <?php 3) Third-best way to take your site offline: 307 redirectThis method uses a code 307 redirect to serve a "Site Closed for Maintenance" page instead of whatever page was requested. The 307 tells search engine crawlers that this is a temporary condition, and the text on the page informs visitors what the problem is. If you are not able to use PHP required by method #2, this is an alternative. Its disadvantage is that you cannot specify a Retry-After time interval. ProcedureIt is basically the same procedure as for Method #2:
4) The fastest way to take your site offline: deny accessThis method uses 2 simple lines of .htaccess code to deny web access. Anyone requesting a file gets a "403 - Forbidden" error page instead. It is very fast. It can be installed in 1-2 minutes, but it is bad because it will confuse visitors and you might lose search engine ranking or indexing for pages that are crawled during the downtime. However, you could use this method briefly during the time it takes to set up and install one of the others. The drawbacks are:
To reopen your site, remove the two .htaccess lines you added. In addition to the purpose described here (closing the whole site), this method is useful wherever you need to make an individual folder inaccessible by web. Just put an .htaccess file with the above 2 lines in the folder you want to close. .htaccess configurations apply to the folder where .htaccess is located and all its subdirectories. That is why, when you put this code in your top level public_html/.htaccess, it closes the entire site. 5) Fast and flexible method - deny everybody except yourselfThis variation of #4 above allows you to access your site normally, but turns away everybody else. Customize the highlighted digits below to your IP address. # AN ALTERNATIVE THAT ALLOWS ONLY YOUR IP ADDRESS You can ask questions and get assistance in the forum. |
|
|
|
|
|