How To Run More Than One Domain On A Single Craft Install
In the Sites guide of the Craft CMS 3 documentation you'll find instructions on how to set up multiple websites on a single Craft install. But there's something missing from that guide that is essential if you set up multiple sites the way a lot of shared hosts will have you do it. The CRAFT_SITE PHP constant is the missing piece.
We like to host all of our client sites at ArcusTech. We've been hosting with Nevin and his team there for years. They have specialized hosting plans optimized for Craft CMS. They handle incoming domain traffic much the same as many web hosts. So in all likelihood, you'll find this post useful even if you are not with ArcusTech.
We're working on launching a complete redesign of solspace.com. We're going to run it on a new hosting account which was set up for our Web Reliability site. We're planning to run the two sites on one Craft CMS install.
The two domains point to CloudFlare name servers. We love the performance gains and security provisions built into Cloudflare. And the ease of pointing domains at different places and changing those is unmatched.
So we set an A record for our dev domain and point that at the IP address for our ArcusTech hosting account.
We're in development. We're using a subdomain to preview our new Solspace website. We let ArcusTech know that we want traffic for that subdomain, let's call it bigkitty.solspace.net, to land in a directory on our VPS hosting account called "solspacecom_html". ArcusTech updates its system to make sure traffic resolving from bigkitty.solspace.net into the dedicated IP address goes to the right place.
We go into our Craft control panel, into the Settings, then into Sites. We then create a new site and call it "Solspace.com". The handle gets created for us as "solspaceCom".
We copy the .htaccess and index.php files from our main site into the "public" folder of our "solspacecom_html" directory.
(And this was the missing bit of information I had a little trouble tracking down.) We add this line to our index.php, the one that we copied into "solspacecom_html/public":
That line tells Craft that we want to serve the "solspaceCom" site from the list of sites belonging to this Craft install.
In that same index.php file we also need to update the CRAFT_BASE_PATH constant so that Craft can find the main system files for the install. On ArcusTech that line will look like this:
"av01234" is your ArcusTech account username. On other hosting accounts that path will be very different, but it's basically the location of the directory containing your main Craft system files.
Sorry for so many steps. I like stuff to be clear.
Craft would love to route traffic for your sites into template directories for you. Most Craft installs route traffic into the "templates" directory. But if you have more than one site running on an install, you might want to name your site template groups. The main site will be named "default". And the other sites should match the handle of the Craft Site you created. In my case, it was "solspaceCom".
There are of course other details pertaining to running more than one domain on a single Craft install, but the above were the key bits I needed for my use case. I hope they help you.