Redirecting visitors to update page during website maintenance

Last updated on December 15, 2009. Tags: , ,

Updating websites is very important in its operations. You need to update its content to ensure that the information are up to date. You also need to update your codes because they might render improperly to newer versions of the commonly used browsers. You might also decide to change the general appearance of your website.

Depending on the intensiveness of the update, you might want to prevent other people from visiting your site while you are updating it. Editing a page will not exactly affect your visitors because they will simply see the new version of the page once you saved the HTML file (or uploaded and overwritten it with a new HTML file).

However, for some edits that will cause some parts of the site or any page to render improperly or malfunction (such as testing and revising the layout), you will need to put up temporarily a page stating that the site is "Under Construction. Your visitors can see only this page and nothing else.

One solution is to change your homepage (index.htm) to an update page, which contains nothing but "this site is currently under maintenance" or something like that. The problem with this approach is that first, visitors can still access your other pages through bookmark or search engine, and second, you cannot do this if the homepage is the one (or one of those) you're going to edit.

The basics

The other solution is to use the .htaccess file to redirect visitors from IP addresses other than your own to the maintenance page. To use this method, create an update page. Let's call it page.htm (you can name it whatever you want as long as you put the same name in the .htaccess), encode a text saying something like "currently under construction" and put it in the root directory.

Next, open your .htaccess and insert the following codes. Insert it at the very top or at the very bottom to ensure that it does not interfere with the other .htaccess functions, if there are any.

order deny,allow
deny from all
allow from 205.97.194.334
ErrorDocument 403 /page.htm

Simply replace the 205.97.194.334 with your own IP address and page.htm with whatever name you provided to your update page. What will happen here is that all visitors trying to visit any page or files in your site, except for you or those using the computer associated with the encoded IP address, will be automatically redirected to page.htm. When a visitor is redirected, s/he will never see the URL of the page.htm, only the location s/he is originally trying to visit.

For example, the visitor tried to visit www.domain.com/faqs.htm, s/he will be redirected to page.htm and will see the texts encoded in page.htm, but the URL in the address bar will still be www.domain.com/faqs.htm and not www.domain.com/page.htm

This .htaccess fuction was tested on CPanel. Theoretically, visitors should not be able to visit the page.htm as well because it's still in the same domain, but in my experiment, the visitors are still redirected to this page. If the code above does not work properly in your site, you can add the following codes immediately below the ErrorDocument line.

<Files page.htm>
allow from all
</Files>

Be sure that you put up this code only whenever you update your site, and don't forget to remove it once your updating is done. I suggest that you save it in a notepad text file so that you can just paste it in your .htaccess whenever needed. You may, however, keep the update page (page.htm). As long as there is no link that leads to it, it should be fine in your server.

If you put this code in the .htaccess and there's no existing page.htm, the default 403 error message provided in the hosting or server will be displayed to your site visitors instead.

Folders and subdomains

Placing this code or .htaccess function in the .htaccess file in the root directory affects the folders and subdomains within it in a different manner. Subdomains will display the page.htm that is in its main folder. Folders, on the other hand, will display the default 403 message and ignore the page.htm in those folders. Accessing a file (whether it exist or not) and folders (that does not exist) within the root directory will display page.htm. However folders that DOES exist displays the default 403 message.

You can also put this .htaccess function in the .htaccess file within a regular folder or a subdomain. Doing it this way will affect only that folder/subdomain and all files and folders under it. This .htaccess function behaves differently in regular folder and in subdomain. Placing it in a folder; it will look for the page.htm that is in the root directory and display it. Placing it in a subdomain; it will look for the page.htm that is in the main folder of the subdirectory and display it.

As in the case of root directory, if no page.htm was provided, it will display a default 403 message provided by the hosting provider. Also, the location of page.htm is never displayed. Only the location the visitor is trying to visit.

Absolute URL for the update page

You can also use absolute URL to tell .htaccess the location of the update page. It is encoded as something like:

order deny,allow
deny from all
allow from 205.97.194.334
ErrorDocument 403 http://www.domain.com/page.htm

<Files page.htm>
allow from all
</Files>

Note that in this case, the second part <Files page.htm> is essential. If you don't put it, you will get a message saying that there's a redirect loop. Despite using absolute URL in the ErrorDocument line, indicating only the name of the file (update page) in this second part is enough

Using absolute URL ensures that you have the appropriate update page and you do not need to concern yourself with the different behaviors of the redirect on a regular folder and on a subdomain. However, this method displays the true URL of the update page. Upon redirection, the URL in the address bar will change into something like http://www.domain.com/page.htm.

Behavior in WordPress-powered sites

This technique does not work in WordPress blogs or any sites working with WordPress CMS. If you created an update page and encoded the code in the .htaccess, the server will ignore the update page and instead display the error message generated by WordPress.

Addendum August 11, 2009: for WordPress-powered sites

To enable this technique to work properly on a WordPress powered blog,  you should use absolute URL as discussed in the "Absolute URL for the update page" section above.  It means you are required to add the second part of the code (<Files page.htm> and all that follows). It also means that the URL at the address bar will also change once the forwarding takes place.

Addendum August 11, 2009: How to test if it works?

You cannot see the redirection working in your own browser because the main idea here is to not allow anyone but you to access the actual website. You can check if it's working by using the W3C  Markup Validation Service. You can do this because the Markup Validation access your site from another IP address.

Whenever you check any pages or folders within the domain (or subdomain or directory depending on where the .htaccess is placed), the Markup Validation will always check for errors in your maintenance page page.htm. If you are using absolute URL, the URL being checked will automatically change.  It can help if you intentionally include minor errors in your maintenance page such as as image tag or meta tag that ends in > instead of /> as you already know what errors to expect.

Posted by Greten on June 7, 2009 under Server Configuration

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.