DEV Community

Nurul Islam Rimon
Nurul Islam Rimon

Posted on

Steps to Use Subdomains on localhost Manually

1. Edit the hosts file
Tell your computer that certain domain names point to your own machine.

Open your hosts file:

  • Windows: C:\Windows\System32\drivers\etc\hosts
  • macOS/Linux: /etc/hosts

Add this at the bottom:

127.0.0.1 myapp.localhost
127.0.0.1 api.myapp.localhost
127.0.0.1 admin.myapp.localhost
Enter fullscreen mode Exit fullscreen mode

Save the file. (You might need admin rights.)

2. Start Your Local Server
Make sure your local server (Node.js, PHP, etc.) is running and listening on localhost.

If you're using Express, you can detect subdomains like this:

app.use((req, res, next) => {
  console.log(req.subdomains); // ['api'] from api.myapp.localhost
  next();
});
Enter fullscreen mode Exit fullscreen mode

3. Open in Browser
Now you can visit:

http://myapp.localhost

http://api.myapp.localhost

http://admin.myapp.localhost

Each one still points to your local machine.

If you don't want to config manually read this. lvh.me could help you to use subdomain in localhost.

Thanks

Top comments (0)