How to Redirect IP Address Range using .htaccess


redirect ip address range htaccess

Sometimes, you may need to redirect a specific IP address range to a different page. You can do this using .htaccess file without accessing Apache config. Here’s how to redirect IP address range using .htaccess.

 

How to Redirect IP Address Range using .htaccess

Here are the steps to redirect IP address range using .htaccess. Before proceeding, please ensure that you have enabled mod_rewrite (.htaccess) in your Apache web server. Here are the steps to do it on:

 

1. Open .htaccess file

Open .htaccess file in a text editor. It is generally located at /var/www/html


$ sudo vim /var/www/html/.htaccess

 

2. Redirect IP address range

Let’s say you want to redirect IP address range (192.168.0.1-192.168.0.255) to a different page. You can do that by using regular expressions in your RewriteCond statements as shown


RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.[0-9]{1,3}$
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [L]

 

If you want to redirect multiple IP address ranges (192.168.0.1-192.168.0.255) and (192.168.10.1-192.168.10.255) then use the OR flag to combine multiple Rewrite conditions


RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.[0-9]{1,3}$ [OR]
RewriteCond %{REMOTE_ADDR} ^192\.168\.10\.[0-9]{1,3}$
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [L]

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!