Redirect everyone except my IP

Posted by 6bytes at 18 October 2008

Category: Apache

When you want to patch your site or just make some amends its a good idea to close the site so that your visitors don’t see the mess you make while patching :)

You can easily do it using .htaccess file.  I assume you know basics of Apache mod_rewrite so I’m not gonna explain each line.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.1$
RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.2$
RewriteRule !holding_page.php|holding_page.jpg$ holding_page.php [R=307,L]

This line:

RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.1$

specifies the IP address you want to let in to see your site.  You can add as many IP addresses as you want.

Last line:

RewriteRule !holding_page.php|holding_page.jpg$ holding_page.php [R=307,L]

specifies two files that can be accessed by anyone (holding_page.php and holding_page.jpg).  Everything else should be redirected to holding_page.php

We don’t want search engines to think we closed down our site.  That’s why the flag 307 which means a temporary redirect.

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)