164

I am writing an web application that behaves differently depending on a url prefix. The format is something like:

   https://myprefix.mycompany.com

The web app behaves differently based on myprefix. My web app extract that part from the URL and act on that.

However, when I test on my local, I use an localhost address:

   https://localhost:1234

I counldn't do something like:

   https://myprefix.localhost:1234

What is the best way for me to test this scenario?

Many thanks

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kevin
  • 5,972
  • 17
  • 63
  • 87

8 Answers8

224

Unfortunately, because localhost is not a proper domain, you can't add a subdomain to it like that. You can, however, trick your computer into thinking it owns a specific domain and test things that way. For instance, if you have a UNIX-based operating system, open (as root) the file /etc/hosts and add a line (or lines) like this:

127.0.0.1    example.com
127.0.0.1    subdomain.example.com

Your computer will now treat both example.com and subdomain.example.com as belonging to itself. If you visit either in your web browser, they will work the same, in principle, as localhost, but your web server will see the correct domain in its Host header.

Matt Patenaude
  • 4,497
  • 1
  • 22
  • 21
  • Many thanks for your answer. This is exactly what I want. However, my web app runs on a port, say 1234. I searched around and people said there is no way to specify a port number in /etc/hosts. What is the best way to specify port? – Kevin Sep 26 '13 at 09:12
  • 13
    You're going to have to just use the port in your URL as usual, e.g., `http://subdomain.example.com:1234/whatever`. The port is entirely separate from the domain (domains are used for identifying the machine, ports are used for identifying which program on the machine to communicate with). – Matt Patenaude Sep 26 '13 at 22:31
  • 1
    Alternatively, if you can run your software as root (for testing), you can just use port 80, which is the default, so you won't have to specify one. – Matt Patenaude Sep 29 '13 at 16:54
  • 4
    One could just list the domain names after the ip address: `127.0.0.1 example.com sub.example.com sub2.example.com...` – automaton Jan 03 '15 at 16:10
  • 9
    This works for windows as well. The host file on windows is located at: `C:\Windows\System32\drivers\etc\hosts`. You will need to copy the file to somewhere else that has lower permissions, (like your desktop), to edit it and then paste it back into the folder (to work around the permissions). – Lindsay-Needs-Sleep Apr 04 '19 at 06:36
  • @Shimmy I added the line `127.0.0.1 m.test.mydomain.com` to the hosts file, I was then able to hit `http://m.test.mydomain.com:8000` (which went to my local server). Did you maybe copy the example in the file and leave the comment character (`#`) in? – Lindsay-Needs-Sleep May 29 '19 at 07:16
  • @sel-en-ium I got it to work now. My issue was with the IIS configurations. Thank you! – Shimmy Weitzhandler May 29 '19 at 07:18
  • I did this and it gave me this error `Invalid Host header` – NwaiwuIsidore Jul 05 '21 at 20:56
  • Surprisingly, on Windows 11 I can type http://w.localhost:3010 into browser, and it will open website on that port, as if it was localhost. In javascript, window.location.host is set as expected. – Pointer Null Jan 26 '22 at 21:48
57

I'm not sure about same behaviour on windows. I'm working on linux mint.

You can use lvh.me:port as a local domain. You can imagine that your project is deployed on localhost:port on this domain.

Instead of sub.localhost:port you've to use sub.lvh.me:port

UPDATE

sub.localhost:port works on Chrome.

Note: Firefox automatically adds www. at the beginning of entered domain that can cause problems with subdomains testing

Matthew Spence
  • 986
  • 2
  • 9
  • 27
Vassily
  • 5,263
  • 4
  • 33
  • 63
  • I can confirm this works on Windows as well. Internet Explorer, Edge, Firefox, and Chrome browsers work flawlessly! – Jose A Apr 20 '18 at 16:59
  • I was using firefox and chrome tip saved my day, thanks! – Azephiar Jan 31 '19 at 11:57
  • 3
    For additional flexibility, you could also use the services nio.io or xip.io. They allow to map any IP address to any subdomain. So e.g. your colleagues could also use the same URL to access your app. E.g. if your workstation's IP address is `172.16.0.42`, you could use `https://myprefix.myapp.172.16.0.42.nip.io:1234` from your PC or from other PCs in your intranet. – mh8020 Jul 09 '19 at 19:39
10

For Windows users, based on this answer and per this comment, you can achieve this by adding ports to localhost via the hosts file that resides at this path:

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

And append lines like the following to it:

127.0.0.1    example.com
127.0.0.1    subdomain.example.com
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
5

One-Line Solution for Windows

Open PowerShell as Administrator and run the following command, replacing sub.mydomain.com with whatever you want.

"`n127.0.0.1    sub.mydomain.com" | Out-File C:\Windows\System32\drivers\etc\hosts -encoding ASCII -append

Breakdown:

  • `n - newline
  • 127.0.0.1 - loopback address
  • sub.mydomain.com - domain name
  • | Out-File C:\Windows\System32\drivers\etc\hosts - pipe the string to the hosts
  • -encoding ASCII - correct encoding
  • -append - append to end of file (important!)
Community
  • 1
  • 1
Richard Dunn
  • 6,165
  • 1
  • 25
  • 36
5

You should be using the .test domain for things like that. That is what .test is for. localhost is not supposed to have any subdomains.

To do so violates the approved RFC standards. localhost has an A record and in IPv6 environments, an AAAA record. All other DNS record types, including SOA are forbidden.

Without an SOA record, it cannot be a zone apex that has sub-records, so no subdomains nor delegations are permitted. Even the recent RFC draft titled Let localhost be localhost is consistent with this.

Lamanus
  • 12,898
  • 4
  • 21
  • 47
MR.X
  • 59
  • 1
  • 1
  • DNS_PROBE_FINISHED_NXDOMAIN this comes after – Paras Sharma Aug 03 '22 at 07:49
  • 3
    Actually, RFC standards [referenced by IANA](https://www.iana.org/domains/reserved) explicitly [allow subdomains for `localhost`](https://www.rfc-editor.org/rfc/rfc6761.html#section-6.3): 'The domain "localhost." **and any names falling within ".localhost."** are special...' Is there another RFC standard that contradicts this? – Grant Jan 27 '23 at 22:08
3

It took me a bit of time to find public wildcard DNS domains pointing to localhost so I'm leaving it here for future reference.

The domain that worked for me is localtest.me. That domain and its sub-domains resolve to 127.0.0.1 and ::1. For example:

$ host localtest.me
localtest.me has address 127.0.0.1
localtest.me has IPv6 address ::1
$ host some-sub-domain.localtest.me
some-sub-domain.localtest.me has address 127.0.0.1
some-sub-domain.localtest.me has IPv6 address ::1

A maintained list of other public wildcard DNS domains that point to localhost can be found in this Gist.

Rob van der Leek
  • 1,486
  • 15
  • 13
  • Note that DNS rebinding protection needs to be turned *off* in the DNS server used to serve any such requests. – Alex Povel Jan 09 '23 at 08:40
2
https://myprefix.mycompany.localhost:1234

This should do the trick. Because localhost is a top-level-domain, it behaves like a .com in production code.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
teremich
  • 21
  • 2
  • This seems non-standard and might lack support :) Windows + NODE-Fetch(v2.6.4) does not map subdomain.localhost to 127.0.0.1 - afaik Linux did not have an issues. Refer: https://stackoverflow.com/a/63515360/3114702 – David Renner Jan 10 '22 at 01:23
  • I have tried this and works (xampp on W11, localhost and subdomain.locahost work). – nickpapoutsis Jan 20 '22 at 07:31
2

From WSL in Windows:

  • First navigate to /mnt/c/Windows/System32/drivers/etc(Navigate cause, you may find more interesting files. Don't play here, but see what do they do)
  • Then do nano hosts(add at very bottom)
    127.0.0.1    random.com
    127.0.0.1    auth.random.com
    
    
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86