Redirect certain subnets to a different site in Apache.
- July 26th, 2013
- Write comment
I did this from the httpd.conf where I have all my virtual directories defined. I just added the following within one of those virtual directory definitions and reloaded httpd. This will redirect anybody on the 192.168.0.0/24 subnet to http://google.com, and the others will proceed to the directory (/my/website/directory).
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^192.168.0.*
RewriteRule .* http://google.com [R=302,L]
In an .htaccess file you would just need to add the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^192.168.0.*
RewriteRule .* http://google.com [R=302,L]
If you need to redirect the cgi-bin, elsewhere:
AllowOverride All
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^192.168.0.*
RewriteCond %{REMOTE_HOST} !^172.16.0.*
RewriteCond %{REMOTE_HOST} !^10.0.0.*
RewriteRule .* http://www.google.com [R=302,L]
Options ExecCGI
Options FollowSymLinks
Order allow,deny
Allow from all