Laravel Forge - Wildcard Subdomains
Posted on June 23, 2014 | By Matt Stauffer
(This is part of a series of posts on Laravel Forge. )
- Getting your first site up and running in Laravel Forge
- Laravel Forge - Logging With Papertrail
- Laravel Forge - Using Environment Variables for Environment Detection
- Laravel Forge - Adding a Queue Worker with Beanstalkd
- Laravel Forge - Scheduling a Cron Job
- Laravel Forge - Adding an SSL Cert
- Laravel Forge - Password Protect a Folder
- Laravel Forge - Wildcard Subdomains
- Laravel Forge - Using Recipes
- Laravel Forge - Hosting on AWS
- Laravel Forge - Forge Plus and Circles
- Laravel Forge - Fixing the CVE-2014-6271 Bash Vulnerability
- Laravel Forge - Global Recipes & the FastCGI Nginx Update
- Starting a blog with Sculpin on Laravel Forge
- Installing a fresh Craft CMS Installation on Laravel Forge
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