Laravel Forge - Wildcard Subdomains

Posted on June 23, 2014 | By Matt Stauffer


Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

If you've ever used a site like Harvest or Slack, you've experienced group-specific subdomains. If your company is named Tighten, you'll get tighten.slack.com, tighten.harvestapp.com, etc.

In order to implement a feature like this on your own Forge-hosted site, you'll need to be able to create a Site that accepts not just one domain but many. Thankfully, this is easy with Wildcard Subdomains.

Allow Wildcard Sub-Domains

When you add a new site, notice that there's a checkbox at the bottom of the form that says "Allow Wildcard Sub-Domains." If you check this box, any subdomain under the domain you enter in the "Root Domain" will be passed to this Site.

Forge Add Wildcard Subdomain Checkbox

How does this work?

Well, imagine you're setting yourself up a Harvest competitor named Schmarvest. You want to set up Schmarvestapp.com so that every subdomain underneath it is reserved for a different user, but you want all of those thousands of subdomains to all be served from one single Laravel app.

Schmarvest

If you just create a new site in Forge for Schmarvestapp.com, and don't use the Wildcard Sub-domains checkbox, you'll see your primary domain (schmarvestapp.com) will work just fine. But if you try to visit one of your subdomains, nothing will happen. tighten.schmarvestapp.com won't go anywhere. You could, if you wanted, add a new Site for each of those subdomains, but they'd each be hosted on a completely separate codebase.

Instead, add a Site for schmarvestapp.com that has the "Allow Wildcard Sub-Domains" option checked. Now if you visit any subdomain under your domain (and if your DNS is pointed to Forge correctly), you'll still always hit your one install.

DNS for wildcard

So, all of this has been assuming you have your domain routing set correctly at your DNS. But what is the appropriate routing?

The most appropriate option is a wildcard A record. So, in your DNS provider's editor, add a new "A" record for your domain.

Set the "Name" field to be * (or *.yourdomain.com, depending on the prompts your registrar gives you for format). Set the "Address" field to that of your Forge Server's IP address. And do whatever you'd like with your TTL.

Schmarvest A Record

That's it! Wait for an hour or so and now all requests to this domain and its subdomains will head over to your Forge server.

Ikadii

That's it! Time for Schmarvest world domaination.


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  forge  •  subdomains