So you want to force the DNS resolution of domain1.com
to another IP (say, domain2.com
's IP) but do not want to do this using /etc/hosts
and would like to do it in the Apache or PHP configuration only.
Well here the problem is that PHP uses your server's DNS settings set in /etc/resolv.conf
and /etc/hosts
as an override, and Apache is just a web-server so it cannot change your application's logic or control it's networking.
What would be cleaner here is to save the IP of domain2.com
in a php config file and call that IP with curl instead of domain1.com
, and you can still add a Host: domain1.com
header to your HTTP call if needed.
Edit : Here is an example using curl in PHP.
Let's say
domain1.com
's IP is 1.1.1.1
domain2.com
's IP is 2.2.2.2
If your goal is to call 2.2.2.2
but with the domain domain1.com
, you just need to change the Host
header of your HTTP request, like this :
<?php
$ch = curl_init('http://2.2.2.2/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: domain1.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);