Add subdomain to localhost URL

HttpUrl

Http Problem Overview


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

Http Solutions


Solution 1 - Http

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.

Solution 2 - Http

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

Solution 3 - Http

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

Solution 4 - Http

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.

Solution 5 - Http

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!)

Solution 6 - Http

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.

Solution 7 - Http

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
    
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevinView Question on Stackoverflow
Solution 1 - HttpMatt PatenaudeView Answer on Stackoverflow
Solution 2 - HttpVassilyView Answer on Stackoverflow
Solution 3 - HttpShimmy WeitzhandlerView Answer on Stackoverflow
Solution 4 - HttpMR.XView Answer on Stackoverflow
Solution 5 - HttpRichard DunnView Answer on Stackoverflow
Solution 6 - HttpteremichView Answer on Stackoverflow
Solution 7 - HttpMaifee Ul AsadView Answer on Stackoverflow