28

I have set up a web application on I am running it on my localhost. That is - http://127.0.0.1:8000/. I have set up sub-domains as well. For example, going to http://blog.127.0.0.1:8000/ should take me to the blog part. However, my browser does not recognize this and says server cannot be found. My OS is MACOSx

If you want to test subdomains on your local host, you need your browser to resolve multiple subdomains as '127.0.0.1'. The way you do this depends on your OS. If you're using linux, you would edit /etc/hosts.

I got this bit of info from another user, however, I have no idea how to do this on my Mac. Any help is appreciated.

darkhorse
  • 8,192
  • 21
  • 72
  • 148

2 Answers2

47

Edit the file /etc/hosts, add the following line:

127.0.0.1 example.com blog.example.com

Then try http://example.com:8000/ and http://blog.example.com:8000/

Dusan Bajic
  • 10,249
  • 3
  • 33
  • 43
  • Works like a charm! But the /etc/hosts file does have a comment that reads "Host Database localhost is used to configure the loopback interface when the system is booting. Do not change this entry." What could possibly go wrong by adding subdomains? – Félix Paradis Apr 16 '19 at 21:38
  • 3
    You should probably not remove existing `127.0.0.1 localhost` entry, but I don't see how adding additional hostnames could have any ill effect – Dusan Bajic Apr 17 '19 at 07:05
12

Must be adding to the file /etc/hosts:

127.0.0.1       localhost blog.localhost lvh.me blog.lvh.me

Example for Vim editor (small overview about working in Vim)

  1. Run to edit file /etc/hosts:

     sudo vi /etc/hosts
    
  2. In Vim press on keyboard button i to switch on edit mode

  3. Add this line:

     127.0.0.1       localhost blog.localhost lvh.me blog.lvh.me
    
  4. Save file:

  • Press button Esc and after press together buttons shift :

  • Now you will see line in which write x and press Enter

Thats all, localhost, blog.localhost, lvh.me and blog.lvh.me are already available for all ports that you are using.

shilovk
  • 11,718
  • 17
  • 75
  • 74