Redirecting domain to sub domain

Jonathan Duane
Jonathan Duane used Ask the Experts™
Hi Guys, i have a domain with lots of subdomains, when people open up a browser based on their IP address i would like if it forwarded onto one of the subdomains so say for instance my ip is and i open up i would like it to redirect to is this possible?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Firstly - are the IP's guaranteed to be static?

If so you build an IP -> subfolder lookup
$lookup = [
  '' => '/folder1',
  '' => '/folder2'

$ip = getIPAddress(); // Custom function see below

$destination = isset($lookup[$ip]) ? $lookup[$ip] : false;
if ($destination) {
  header('location: ' . $destination);
} else {
   // handle error

function getIPAddress(){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        $ip = $_SERVER['REMOTE_ADDR'];
    return $ip;

Open in new window

Note: The above code is untested but it demonstrates the concept.

The getIPAddress() function was obtained from here other examples are here
Most Valuable Expert 2017
Distinguished Expert 2018
It is possible to do this with an .htaccess rewrite rule but this is not recommended if you are going to be adding / removing clients regularly.

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^xxx\.xxx\.xxx\.xxx
RewriteRule  .* /folder1 [L]

Open in new window

This is not a recommendation - this is just a "how to" I am sure there are some ramifications of doing this from an SEO perspective that you would need to take into account if SEO is important.
David FavorFractional CTO
Distinguished Expert 2018

Both of Julian's suggestions will work.

And if you have many users, your main budget may go toward dealing with visitor problems related to your IP routing, depends on your exact system.

Now the big question is what you hope to accomplish.

Keep in mind these days...

1) Many people use VPNs, so their IPs will constantly change.

2) Most people use cell devices, so their IPs may change as they bounce between cell towers + networks, depending on many factors.

5G is another consideration. No one knows yet, as implementations are rare + vary a great deal.

3) You can no longer make any Geo determination for a visitor on an IP, because of #1 + #2.

Suggestion: If required, open another question describing what you hope to accomplish + ask for design assistance.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial