Add subdomain to localhost URL

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.


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

Tags:

Http

Url

Web