You need to set your /etc/hosts or C:\Windows\system32\drivers\etc\hosts (as administrator) to reflect the "subdomain". So add 127.0. 0.1 dev. localhost to either file (depending on your platform).
While a domain (or domain name) is used to create a memorable address for a website, a subdomain (for example, mysubdomain.mydomain.com) can be used to do the same for a specific website section.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With