Iteratively developed web projects are constantly updated. Sometimes the file structure has to be changed. Sometimes a new table is added to the database schema. Sometimes the data has to be massively changed. In a simpler case, HTML templates and style is changed. Usually, a more complex upgrade takes half an hour or even an hour, and the visitors should see “Under Construction” page instead of the normal site during the process, because the nobody should see broken view or corrupt data by accident while writing something to the database at that time.
A good practice for Apache web server users is to have a couple of configuration files which would be activated alternately, depending on whether the website is accessible to everyone, or is in the upgrade state. Once Tomas gave me an idea to create such configuration for the upgrade cases that others saw “Under Construction” page and I could browse the content and test if everything looks alright. So did I.
Apache lets you set the web server configuration on the directory level using .htaccess files. For the important projects I have files .htaccess_live and .htaccess_under_construction containing different settings in the root directory of the website. When I need to change the state, I copy the appropriate configuration to the .htaccess file:
cp .htaccess_under_construction .htaccess
or
cp .htaccess_live .htaccess
The content of the Apache configuration file .htaccess_live is something like this:
# index.html and index.php represents the directory by default
DirectoryIndex index.html index.php
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# If the page was accessed by example.com, then redirect the request to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
</ifmodule>
Whereas the content of .htaccess_under_construction is this:
# index.html and index.php represents the directory by default
DirectoryIndex index.html index.php
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# If the page was accessed by example.com, then redirect the request to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
# if the IP address of the visitor is not 1.2.3.4 and the requested directory is not media,
# then redirect the request to the file temporarily-offline.html
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteCond %{REQUEST_URI} !/temporary-offline\.html$
RewriteCond %{REQUEST_URI} !^/media
RewriteRule .* /temporary-offline.html [R=302,L]
</ifmodule>
These configurations use the mod_rewrite module. RewriteCond defines the conditions which should be met to execute the redirect of request set by RewriteRule. The parameters in the brackets have the following meaning:
- NC (no case) – use case insensitive comparison.
- R=301 – redirect the request with code 301 “Moved Permanently”.
- R=302 – redirect the request with code 302 “Moved Temporarily”.
- L (last rule) – this is the last rule, so don’t execute the rest.
You can find your own IP address in one of the plenty What’s My IP services.
Now it’s clear how to technically set the temporary “Under Construction” view. The next thing, which seems quite important to me, is to show an appropriate temporary page. I personally like such error and system-message pages which describe the problem in a non-technical and visual way like twitter.com does, or the ones showing a video or something interactive to make me busy while the upgrade is being done.






