Laravel Forge - Wildcard Subdomains
Posted on June 23, 2014
!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.
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.
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.
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
*.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.
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.
That's it! Time for Schmarvest world domaination.
Comments? I'm @stauffermatt on Twitter
Tags: laravel • forge • subdomains
This is part of a series of posts on Laravel Forge:
May 15, 2014 | laravel, forge
May 16, 2014 | forge, laravel, papertrail
May 19, 2014 | laravel, forge, environment
May 23, 2014 | laravel, forge, queue, beanstalkd
Jun 2, 2014 | laravel, forge, cron
Jun 2, 2014 | forge, laravel, ssl
Jun 17, 2014 | forge, htpasswd, nginx
Jun 23, 2014 | laravel, forge, subdomains
Jul 9, 2014 | forge, laravel, recipes
Jul 25, 2014 | laravel, forge, aws, hosting
Sep 17, 2014 | laravel, forge
Sep 24, 2014 | laravel, forge
Sep 25, 2014 | laravel, forge, recipes
Dec 24, 2014 | forge, sculpin, fiveMinuteGeekShow
Dec 29, 2014 | laravel, forge, craft