.htaccess – Redirect Requests From Specific IP

The .htaccess file is a configuration file for the Apache web server, that can be used to redirect requests from specific IP addresses to another resources.

In this short note i will show how to redirect requests from one or several IP addresses via .htaccess file to a specific web page or another website.

Cool Tip: Deny access from specific IPs via .htaccess! Read more →

Redirect Based on IP

To redirect all requests from a specific IP to a specific web page, add these line to your .htaccess file:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} 192.16.0.100
RewriteRule $ /only-for-you.html [R=301,L]

The snippet above redirects all requests from the IP 192.16.0.100 to the only-for-you.html page located in the same directory as the .htaccess file.

You can also redirect requests from several IP addresses:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} 192.16.0.100
RewriteCond %{REMOTE_ADDR} 192.16.0.101
RewriteRule $ /only-for-you.html [R=301,L]

Redirect requests from a specific IP to another website:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} 192.16.0.100
RewriteRule $ https://www.fbi.gov [R=301,L]

R=301 in the examples above means the permanent redirect and the flag L means Last and marks the end of the particular rewrite rule.

Leave a Reply