How to serve international TLDs using Laravel Valet

Discover how to configure dnsmasq in Laravel Valet to support custom TLDs like .com for local development on your machine.

Laravel Valet Logo

And by “valid TLDs” I mean those that are recognized by 3rd-party services and APIs as international domains (.com, .org, etc.).

I’m not talking about the tunneling – I don’t need to serve a local site outside. I just need to work on a project locally that managed on my machine by Laravel Valet and has a non-.test top-level TLD. In my case, I wanted a .com.

I reached out to a friend of mine, Max (check out his site – WP-Punk), and he provided a great vector how to achieve this.

So below you will find a guide how to do that in Valet (4.8.7 at the time of writing).

First of all, if you use a great tool called PHP Monitor – do not use it during this process, it may not support this setup. You will need to work with Valet commands directly (only valet link and valet restart will be required).

Configure dnsmasq

Firstly, you will need to configure dnsmasq which allows Laravel Valet to proxy all requests on the *.test domain to point to sites installed on your computer. For our case, we will want to instruct dnsmasq to also support other custom TLDs.

Go to the ~/.config/valet/dnsmasq.d/ directory, and duplicate the tld-test.conf file.

Rename it to tld-example.abc.com.conf, and replace its content with this configuration:

address=/example.abc.com/127.0.0.1
listen-address=127.0.0.1Code language: JavaScript (javascript)

I think it’s self explanatory. If you want to support subdomains, add a dot (.) before example on the first line.

I use a specific domain here to decrease chances of compatibility issues in the future, but if you are brave, you can do that for youtube.com too :)

Important: Restart the valet using valet restart.

Create new example.abc.com domain

For that, you need a directory that will server your files. So let it be like this:

mkdir ~/Projects/example
cd ~/Projects/example
echo 'example.abc.com' > index.phpCode language: JavaScript (javascript)

Now, as Laravel Valet already knows about the new rule for dnsmasq for custom TLD, let’s create a new link:

valet link example.abc.comCode language: CSS (css)

Valet will follow it’s own procedure to create a correct Nginx configuration file, and a symlink in the Sites directory. Ignore the fact that files are called like this: example.abc.com.test – because of the dnsmasq configuration, it’s irrelevant.

If you want, you can secure it too with this command:

valet secure example.abc.comCode language: CSS (css)

Instruct computer to look for local sites

Because by default all public TLDs (like .com in this example) will go to the internet, we need to instruct our computer to look for it locally. So let’s add this line to /etc/hosts file:

127.0.0.1 example.abc.comCode language: CSS (css)

And that’s it.

Test your custom TLD

Now, when you open https://example.abc.com locally:

  1. computer will route the request to 127.0.0.1,
  2. Valet will intercept it based on the newly created dnsmasq rule,
  3. it will look for the local registered site to serve from its ~/.config/valet/Sites directory,
  4. and effectively it will serve your ~/Projects/example/index.php file to the browser.

And now you can use this URL in your other local sites, or submit it as a test URL (non-public) to Google Projects or other services.

Subscribe for Updates

Stay up to date with my latest blog posts, projects, and more!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *