Is there a way to prevent apache from connecting to a specific domain e.g. somesite.com? I have a PHP script that trying to connect to specific host, and I would like to prevent that.

  • Don't run your script?
    – MrWhite
    Aug 20, 2012 at 8:49
  • @w3d If you mean not running the script at all, that's not necessarily practical. I can see a case for this where an application is trying to "phone home" for example. I may want to use the app, just don't want it reporting our server(or anything else) back to the dev.
    – Su'
    Aug 20, 2012 at 9:42
  • 2
    @Su' Yes, you certainly have a point. My remark was perhaps a bit facetious.
    – MrWhite
    Aug 20, 2012 at 10:18

3 Answers 3


If the PHP script is accepting parameters in the URL such as http://www.example.com/thescript.php?somesite.com you may be able to create a re-write in your .htaccess so anytime that URL is loaded it could simply refresh example.com

Or use an iptables rule such as iptables -A OUTPUT -d -j DROP where the 0's are put the IP of the domain you want to block.

Having the php script modified would only be a few bucks on most freelance sites since it's pretty basic to add an if conditional statement that says if the URL entered is somesite.com don't execute.


I know this is somewhat hackish but it is dead simple to implement. If the script is attempting to connect to a dns based url (not an ip address based url), you could simply set your host file to point to for that dns name only. This assumes you have no intention on browsing that particular site.

# in /etc/hosts  somedomainIwanttoAvoid.com

As a bonus, you can setup a virtual host locally to catch these requests and handle them however you want.



Here is an example of how to do this with htaccess. Note that it is relatively easy to circumvent the referer. The same website also has an example on blocking by IP.

Correction: I found this tutorial on IPTables. Perhaps that can get you started.

  • This prevents visitors to your site from some other site. The question goes the other direction: it's about preventing an on-site script from communicating with an external server.
    – Su'
    Aug 20, 2012 at 9:45
  • hi. thank for reply but you didnt understand me. i need to block outgoung conenction to specified host, not incoming. Aug 20, 2012 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.