How to use custom domains with different TLDs on Laravel Valet
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.
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:
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
sudo vim /etc/hosts
and add it there.
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.
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 :)