How to create local domains to test my websites and apps?

Asked

Viewed 3,911 times

5

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

I thought it would be possible by the hosts file on

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

doing something like:

localhost:8084 webtest.local

But when trying to access, receive the error that it is not possible to access the site and list the DNS

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 it to go to localhost:8888, for example, and not only for 127.0.0.1:8888.

There’s a very dramatic way to do that?

2 answers

12


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


Do not fit door specifications on 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", simply you provide a name and it returns an IP, no more, no less. The doors anyway will have to be typed in the browser when accessing the resource, if different from the standard.

The biggest advantage of you creating multiple local domains to point to the 127.0.0.1 in IPV4 it is precisely to be able to test several independent addresses without needing doors or paths different from the real path of the hosted application.

Configuring:

The HOSTS file on Windows 7 and larger is on

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 subjects 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 set the server to port 80, which is the default when omitted to HTTP, and 443 when omitted to HTTPS

If you want port redirection, you need a server answering at port 80 local, to check the names and redirect properly with some script your, but if it is to do this, it pays to simply point out the Virtual Hosts from the server each to the right directory of the sites.

Related:

Configure domain names for the same site (different files) IIS WS2012

Configuring Virtual Hosts in Apache


CORS

One advantage of doing this is that in addition to everything you have control over CORS as you 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’re doing, you need the steps mentioned above, but if you just want to do a quick test on the local machine, someone has kindly set up a DNS service on lvh.me which always points to the local machine.

So without setting anything up, you can already quit using this address:

http://lvh.me/

that it already points to the local address.

And you can test more than one domains smoothly:

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, can point to a local network IP and configure multiple network hosts to the same address, which is not possible in this case here.

To specify Ips, a good alternative is xip.io. Instead of you 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 you wish.

0

The problem is the door!

Remove port from hosts files:

Of: localhost:8084 webtest.local

To: 127.0.0.1 webtest.local

When accessing through the browser, use the port again:

http://webtest.local:8084

  • 3

    The respsota is the same as @Bacco, I would like to give more details?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.