Supporting multiple TLDs in Laravel Valet

9th Sep '193 of your Earth minutes

Supporting multiple TLDs in Laravel Valet

Valet is great.

Occasionally though I need an app to respond on a different TLD to the default one. And there are even times when it makes sense to have an app respond on multiple TLDs (e.g. to prove some multi-domain functionality).

You may have some secure assets hosted by a third-party that requires domain verification (FontAwesome Pro). In development, that may mean it’s easier to be using the same domain as other devs on the team than add loads of different dev domains to the service’s whitelist.

In the current release of Valet, this kind of functionality isn’t natively available. Even in third-party packages like Valet+, this isn’t supported. But with a few quick changes it’s easily possible. Let me show you how.

Dnsmasq

First we’ll need to update the Dnsmasq config so that it will handle multiple TLDs.

Turns out this is really easy. Valet holds some config in ~/.config/valet/dnsmasq.conf.

In there we just need to add some config for each TLD we want to support. Add the following two lines for every TLD you want to support. Replace {tld} with the TLD, unsurprisingly.

1address=/{tld}/127.0.0.1
2listen-address=127.0.0.1

Then restart Dnsmasq:

1sudo brew services restart dnsmasq

Valet

Now we just need to update the Valet code to support multiple domains.

Firstly, we’re going to add some values to our ~/.config/valet/config.json. I’ve opted for simply adding keys rather than changing any existing one. I’ve added a tlds array. This will be used later.

1{
2 ...
3 "tlds": [
4 "tld1",
5 "tld2"
6 ]
7}

Then we just need to edit some Valet files and run some commands.

All of these edits I’m making directly in my globally-installed Valet (~/.composer/vendor/laravel/valet/).

1// server.php, line 70
2$siteName = basename(
3 // Filter host to support wildcard dns feature
4 valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
5 '.'.$tld
6);
7
8// change to:
9foreach ($valetConfig['tlds'] as $tld) {
10 if (strpos($_SERVER['HTTP_HOST'], '.'.$tld) === false) {
11 continue;
12 }
13
14 $siteName = basename(
15 // Filter host to support wildcard dns feature
16 valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
17 '.'.$tld
18 );
19}

This just allows the server to respond to whichever one of the TLDs we want to support.

Now let’s update the CLI so we can generate and remove SSL certs for all of the TLDs (valet secure and valet unsecure commands). Firstly, secure:

1// cli/valet.php, line 145, secure command
2$app->command('secure [domain]', function ($domain = null) {
3 $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
4
5 Site::secure($url);
6
7 Nginx::restart();
8
9 info('The ['.$url.'] site has been secured with a fresh TLS certificate.');
10})->descriptions('Secure the given domain with a trusted TLS certificate');
11
12// change to:
13$app->command('secure [domain]', function ($domain = null) {
14 $urls = [];
15
16 foreach (Configuration::read()['tlds'] as $tld) {
17 $url = ($domain ?: Site::host(getcwd())).'.'.$tld;
18
19 Site::secure($url);
20
21 $urls[] = $url;
22 }
23
24 Nginx::restart();
25
26 $urls = implode(', ', $urls);
27
28 info('The ['.$urls.'] site has been secured with a fresh TLS certificate.');
29})->descriptions('Secure the given domain with a trusted TLS certificate');

And now the unsecure command:

1// cli/valet.php, line 158, unsecure command
2$app->command('unsecure [domain]', function ($domain = null) {
3 $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
4
5Site::unsecure($url);
6
7Nginx::restart();
8
9info('The ['.$url.'] site will now serve traffic over HTTP.');
10})->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');
11
12// change to:
13$app->command('secure [domain]', function ($domain = null) {
14 $urls = [];
15
16 foreach (Configuration::read()['tlds'] as $tld) {
17 $url = ($domain ?: Site::host(getcwd())).'.'.$tld;
18
19 Site::secure($url);
20
21 $urls[] = $url;
22 }
23
24 Nginx::restart();
25
26 $urls = implode(', ', $urls);
27
28 info('The ['.$urls.'] site will now serve traffic over HTTP.');
29})->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');

Finally, run the following command:

1valet restart

Enjoy!

#notadesigner • #sometimesitworks

All content licensed CC BY-SA 4.0  •  Code highlighting by Torchlight