Is there some de-facto way for rewriting certain hostnames to other ones? Something like /etc/hosts for host to host instead of ip to host. Is this possible or should I create a local dns cname for that host?

Update regarding the comments

I want to use the local name that resolves to remote domain. The browser is just an example. I'm actually writing an ios app that requests resources from the internet, but I'd like to use local name for simulator-only runs. So to put it in another way I want my app to request http://localalias/, but that system would actually fetch http://remotehost.com/.

  • Do you control the host software and DNS zone for remotehost.com? In other words could you configure the web server at that location to respond to requests coming in under a different name?
    – Caleb
    Jun 21 '11 at 9:41
  • No. I have no control over the remote host whatsoever.
    – Eimantas
    Jun 21 '11 at 9:47
  • Then see my answer below. Even if you were to configure your local network with a DNS hack to connect devices to the remote site using a local name, a properly configured remote site will NOT respond because of a site name mis-match. You will need to implement a proxy solution instead.
    – Caleb
    Jun 21 '11 at 9:50
  • @Caleb: whether the remote host responds to a different name depends on the remote host configuration - you cannot know without trying. It's entirely possible that it does, although you're right that it's becoming less and less common these days. Jun 21 '11 at 10:06
  • What you need is to put an HTTP proxy between your application and the Internet. I don't have a recommendation offhand; Squid can surely do this and is available for OSX but it's probably overkill. Jul 21 '11 at 16:19

DNS CNAMEs would be the de-facto way to do this.

Edit: In light of comments below...

I don't think you'll be able to do what you are trying to do. You're trying to trick the browser or some other program into thinking something is an address it's not. The problem is that something is also going pass the name of the resource it wants so that the remote server knows what site to dish up. More than one site could be hosted on a given ip address. The browser sends the site it wants as part of it's request, just re-routing the traffic via a DNS hack is not going to be enough because the browser would be asking for a resource name that the remote site doesn't know anything about.

You will need to setup a full proxy system on your local system. It needs to either respond with standard browser headers to redirect you to the remote resource, or it needs to fetch the remote resource itself then pass through the data. This could be done with apache, squid, or any number of other proxy and http hosting solutions. If you give more details of your scenario we could be more specific.

  • how and where should I go about this?
    – Eimantas
    Jun 21 '11 at 8:31
  • Are you trying to create aliases for domains you control or other domains around the net? Do you have bind running anywhere already?
    – Caleb
    Jun 21 '11 at 8:33
  • I have a domain (say www.ontheinternet.com). I want to access it on my browser via ontheinternetbutlocal. I'm using OSX. I think it has some sort of mDNSResponder.
    – Eimantas
    Jun 21 '11 at 8:38
  • Ok that's a different question than I thought you were asking. See my edited answer.
    – Caleb
    Jun 21 '11 at 8:58
  • Please see my updated question. I don't think I can be more specific .)
    – Eimantas
    Jun 21 '11 at 9:36

Each line of /etc/hosts has the format:

ip fqdn aliases


  • ip is the IP address of a host;
  • fqdn is the DNS fully-qualified name (the one that you would put in DNS A and PTR records)
  • aliases is a (possibly empty) list of alternate names for the same host (these would be CNAME records in DNS)

So, you can add any alternative/local name in the aliases part. For example, if you have installed a webapp on but want to test it using the production site name http://webapp.example.org, you could write this into /etc/hosts: localhost.localdomain localhost webapp.example.org
  • Unfortunately I think this guy is trying to do the exact oposite of this -- tricking clients into using a name that looks like a local site to access a remote resource that he doesn't control.
    – Caleb
    Jun 21 '11 at 9:00
  • @Caleb: The comment above says "I want to access www.ontheinternet.com on my browser via ontheinternetlocal". I read this as wanting a local-only alias... Let's wait for the OP to clarify the question. Jun 21 '11 at 9:28
  • We do need clarification, but even the way you read it, you could create an alias with the ip and local name in /etc/hosts, but it would still fail. In your example the local webserver could be configured to respond to requests for webapp.example.org. Doing this backwards, the site www.ontheinternet.com isn't going to answer requests for ontheinternetlocal.
    – Caleb
    Jun 21 '11 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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