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

For my tests I would like to create a redirect from any domain to localhost:porta on local computer, on Windows.

I thought it would be possible by the hosts file in

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

Doing something like:

localhost:8084 webtest.local

But when trying to access, I get the error that it is not possible to access the site and list the DNSs

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 it, I would need if it were for localhost:8888, for example, and not just for 127.0.0.1:8888.

Is there a less dramatic way to do this?

Author: Bacco, 2016-12-10

2 answers

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


No port specifications fit in hosts, and there is no functionality related to redirects or anything more complex than a pair of IPs X names.

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

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

Setting:

The HOSTS file on Windows 7 and higher is in

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

In Linux distros it is common for them to be in

/etc/hosts

The syntax of HOSTS 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 when accessing the address. If you want to access without putting port, you need to configure the server to 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 attending on local port 80, which checks the names and redirects properly with some script saddle, but if it is to do this, it pays to simply already point the Virtual Hosts of the server each to the right directory of the sites.

Related:

Configure multiple domains for the same site (different files) IIS WS2012

Configuring Virtual Hosts not Apache


CORS

An advantage of doing this is that in addition to everything you get to have control about CORS as it would with 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 are doing, you need the steps mentioned above, but if you just want to do a quick test on the local machine, someone has done the nice to create a DNS service in lvh.me that always points to the local machine.

So, without configuring anything, you can already exit using this address:

http://lvh.me/

That it already points to the local address.

And you can test more than one domains without problems:

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

Just do not recommend for more definitive things, because it may be that one day the address stops working.

Also, on HOSTS you can point not only to 127.0.0.1, you can point to one local network IP and configure multiple network machines to the same address, which is not possible in this case here.

To specify IPs, a good alternative is the 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 one can imagine, will solve for the address 10.0.0.1. To use any other address, simply adjust the URL as you wish.

 12
Author: Bacco, 2017-04-13 12:59:35

The problem is the door!

Remove port from hosts files:

From: localhost:8084 webtest.local

For: 127.0.0.1 webtest.local

When accessing from the browser use the port again:

Http://webtest.local:8084

 0
Author: Igor Santana, 2016-12-10 17:08:11