Arrow

Temporarily redirect site visitors with .htaccess

First published on August 4, 2008

Even if you follow the good practice of first making changes to a development server, you still need to implement your site upgrades or code tweaks on a live site. During your implementation time, you probably don’t want people to visit your site so that they don’t see revealing errors or cause some data damage.

A quick solution is to make a simple “please come back shortly” page (let’s call it temp_upgrade.htm) and redirect all site visitors to that page until you’re ready. The redirect part can be handled by a text file named .htaccess, placed in the root directory of your site. If you are using a content management system, this file probably already exists. .htaccess has all sorts of powerful uses for authentication, error responses, URLs, and caching.

For this solution, we will alter URLs via a rewrite rule at the top of your .htaccess file. Supposing that your IP address is 65.98.113.34 (find yours by going to whatismyip.com):

RewriteEngine On
RewriteCond %{REQUEST_URI} !(temp_upgrade\.htm)
RewriteCond %{REMOTE_ADDR} !(65\.98\.113\.34)
RewriteRule ^(.*)$ temp_upgrade\.htm [L]

The first line enables the rewriting engine (if it is not already enabled). The second line applies the rule for all pages on your site except for the “please come back shortly” page, and the third line applies the rule for anybody who visits your site except for you. The fourth line is the actual rule, which redirects all site visits to the desired page.

Therefore, you can work on your site, viewing and testing it as per normal, while everybody else will be redirected somewhere else. When you’re ready to reveal your site again, either remove those lines from the .htaccess file or comment them out — add a hash (#) at the beginning of each line — so that you’ll have a model there for next time.

Note that this solution has a drawback in that if you share a network with other computers that have the same IP address as you, those computers will still be able to access your site. You can mitigate this in many ways (among other things, adding password protection and temporarily moving the site’s files), but that’s a whole other discussion.

Arrow

One Response to “Temporarily redirect site visitors with .htaccess”


  1. lykasd says:

    why not use 127.0.0.1 as the ip

    Reply from Peter: That would work if you were browsing the site from the same server on which it is hosted.

Speak your mind

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word