How do I redirect a domain to another domain locally?

26

5

My /etc/hosts file looks like this:

127.0.0.1       localhost
10.20.7.67      testsitealpha.dev
othersite.dev   testsitebeta.dev

The first two work. The last one doesn't. Why not? How do I make it work? The reason I am doing this, is because I have a test server (othersite.com), which is on the local network, but it's ip can vary. e.g. 10.20.7.98 one day, 10.20.7.35 another, etc.

So, how do I make my system always resolve testsitebeta.dev to the same ip as othersite.dev?

Benubird

Posted 2013-05-30T09:01:09.903

Reputation: 381

Answers

11

As Chris already wrote, the problem is that "othersite.dev" is not a number. The format you need is IP hostname1 [hostname2] [hostname3].

The deeper problem however is a misunderstanding about the host file and possibly about DNS in general. Basically the host file was not designed for redirections. It was a simple solution back from when CPU cycles where expensive. It lacked features such as the ability to adjust to rapidly changing IP addresses without manually needing to edit a file. These problems were solved by switching from the host file to network based resolver system.

The real answer to your problem therefore is not to use /etc/hosts, but to use this hierarchical distributed naming system instead. You can do that in several ways. One way is the CNAME as suggested by Chris. Another way would be to give the second server a fixed IP address. Both a real static address, or make a reservation in the DHCP server.

Hennes

Posted 2013-05-30T09:01:09.903

Reputation: 60 739

3In that case you want to insstall a local DNS server and use that as resolver. – Hennes – 2014-07-16T07:54:36.370

4

Local DNS server seems to be the solution. For future visitors, I found this helpful: http://superuser.com/q/45789/75287

– Benubird – 2014-07-16T10:40:58.970

@Benubird: I am also looking for the same solution. But couldn't get it. Can you explain a little further how you setup? – Jay Chakra – 2017-04-08T08:33:29.230

2Not the kind of answer I'm looking for. This is for development; I don't want to modify any other machines, since the domain name (testsitebeta.dev) does not need to be accessible from anywhere but my local. I just want a way to tell my computer "treat name X like it was Y". – Benubird – 2013-05-30T10:16:41.847

3

Format of /etc/hosts is "IP" "Hostname" - like 127.0.0.1 localhost

You need a CNAME in your local DNS server to achieve what you need. You could get the IP of the "othersite.dev" and link it to "testitbeta.dev" like you did with "testitalpha.dev" but when that IP changes you will have to change your /etc/hosts file as well.

Chris

Posted 2013-05-30T09:01:09.903

Reputation: 1 766

1

If othersite.com has dynamic IP, but is always accessible by name you have to rely on its registered DNS by always referencing it by name.

Your solution is to make testsitebeta.dev a redirecting URL for othersite.com.

ServerName testsitebeta.dev Redirect / http://othersite.com

This way othersite.com always works (not breaking it with bad host entry) and testsitebeta.dev is always redirected to othersite.com by resolving the name to the right IP.

user557515

Posted 2013-05-30T09:01:09.903

Reputation: 11