How do I create local domains to test my sites and applications?

5

For my tests, I'd like to create a redirection of any domain to localhost:porta on a local computer under Windows.

I thought it would be possible for the hosts file in

C:\Windows\System32\drivers\etc\hosts

doing something like:

localhost:8084 webtest.local

But when trying to access, you get the error that you can not access the site and list the DNS's

ipconfig /displaydns

Appears:

webtest.local
----------------------------------------
O nome não existe.

I tried to update a possible cache by doing:

ipconfig /flushdns
ipconfig /release
ipconfig /renew

And to complicate matters, I'd need it to be localhost:8888 , for example, and not just 127.0.0.1:8888 .

Is there an untrained way to do this?

    
asked by anonymous 10.12.2016 / 16:19

2 answers

12
  

If you want a simple and ready solution for quick tests, go straight to the end of the question


It does not fit port specifications in hosts , and there is no functionality related to redirects or anything more complex than a pair of IPs x Names.

hosts is just an "index", you simply provide a name and it returns an IP, no more, no less. Ports will anyway have to be typed into the browser when accessing the feature, if different from the default.

The biggest advantage of creating multiple local domains to point to 127.0.0.1 in IPV4 is to be able to test multiple independent addresses without needing ports or paths other than the actual path of the hosted application.

Configuring:

The HOSTS file on Windows 7 and larger stays at

C:\Windows\System32\drivers\etc\hosts

In Linux distros, it's common for

/etc/hosts

The HOSTS syntax is this:

127.0.0.1 localhost
127.0.0.1 webtest.local
127.0.0.1 outroteste.local

You can group by subject if you prefer to organize:

127.0.0.1 localhost
127.0.0.1 webtest.local outroteste.local maisumteste.local

The port you specify on the web server and access the address. If you want to access without setting port, you need to configure the server for port 80, which is the default when omitted for HTTP, and 443 when omitted for HTTPS

If you want port redirection, you need a server running on local port 80, which checks the names and redirects properly with some script of yours, but if it is to do so, in> the server each to the right directory of the sites.

Related:

  

Configuring Multiple Domains for the Same Web Site (Different Files) IIS WS2012

  

Configuring Virtual Hosts in Apache


CORS

One advantage of doing this is that you have control over CORS as you would any conventional domain, which is not guaranteed using localhost , depending on the browser .

To learn more about CORS:

  

What is the meaning of CORS?


Quick solution using third-party service

If you want control over what you're doing, you need the steps mentioned above, but if you just want to do a quick test on the local machine, someone was kind enough to create a DNS service in lvh.me that always points to the local machine.

Then, without configuring anything, you can exit using this address:

http://lvh.me/

It already points to the local address.

And you can test more than one domain without problems:

http://jamestk.lvh.me/
http://stackoverflow.lvh.me/
http://qualquercoisa.lvh.me/

I just do not recommend it for more definite things, because it might be that one day the address will stop working.

In addition, in HOSTS you can point not only to 127.0.0.1 , you can point to an IP of the local network and configure several machines of the network to the same address, which is not possible in this case here.

To specify IPs, a good alternative is xip.io . Instead of using the base address, you include the IP you want as part of the address:

http://nomequeeuquiser.10.0.0.1.xip.io

or simply

http://10.0.0.1.xip.io

which, as you can imagine, will resolve to the address 10.0.0.1 . To use any other address, simply adjust the URL as desired.

    
10.12.2016 / 17:21
0

The problem is the door!

Remove the port from the hosts files:

From: localhost:8084 webtest.local

To: 127.0.0.1 webtest.local

When accessing the browser, use the port again:

link

    
10.12.2016 / 18:08