How to use custom domains with different TLDs on Laravel Valet

Published on May 19th, 2018

If you use Laravel Valet, a Laravel development environment for Mac minimalists, you have the possibility to set one top-level domain. DnsMasq resolves all domains with that top-level domain to 127.0.0. automatically. But what if there is the need for several different TLDs? Let me show you.

What's the reason behind it

Here is my case. A big project of mine has the requirement to load specific language and therefore project configuration based on the given TLD. To get as near as possible the production environment, I wanted to use real TLDs. And as you know, I like the idea, to use the real domains with a local subdomain prefix. Let's try to achieve that.

Configuration

Get rid of DnsMasq and ignore it. What I do now is really simple. Use the valet park command in your the directory, where all your sites live.

Naming project folders

Here is the trick:

Use as a project/webpage directory the domain you would use. In my case, I have directories like this:

  • ~/Code/local.domain-A.com
  • ~/Code/local.domain-A.co
  • ~/Code/local.domain-A.io
  • ~/Code/local.domain-B.com
  • ...

Resolving the domains

The only thing that's still missing is, that these domains are not resolved to localhost. Easy enough. Add it to the /etc/hosts table.

sudo vim /etc/hosts

and add it there.

/etc/hosts
127.0.0.1 local.domain-A.com
127.0.0.1 local.domain-A.co
127.0.0.1 local.domain-A.io
127.0.0.1 local.domain-B.com

That's it. Now go to your browser and call one of your domain.

Conclusion

This is a very easy way to use customized domains. I am not sure if it's necessary to add these host entries. I got the hope that this is also with DnsMasq possible, but I didn't find a way to do it, to resolve local.* subdomains to 127.0.0.1. If you know a way, please let me know :)

Bobby Bouwmann and I are writing a book called "Laravel Secrets".
You will learn everything about the undocumented secrets of the popular Laravel framework. You can sign up using the form below to get early updates.
      Visit laravelsecrets.com to get more information.