Allowing User Custom Domains in Laravel with Cloudways

Feb 23, 2020 — 2 mins read

When building rokk.it, I wanted to give users' the ability to add their own domains for URL redirections. A simple idea proved to be more complex than I thought, and I ended up using a tutorial, which can be found here, that allowed me to achieve my goal of having dynamic domain routing quite easily.

Laravel supports subdomain routing out-of-the-box, however getting full domains to route dynamically is not offered. The author of the article above created a work around for this by modifying the boot method of his RouteServiceProvider.php file.


Once this is added to your RouteServiceProvider file, the domain route group can be used in your routing file, which will then be passed into your controller as a parameter named domain. Visit this link for a better explanation of this method.

Once I got the dynamic domain routing working by pointing test domains to rokk.it using CNAME records, I quickly realized I would need to add the new domains to my app in Cloudways. First the new domain has to be added to the domains section of the application management page and then to the SSL certificate. My rokk.it app enforces HTTPS via Let's Encrypt, so the domains pointing to rokk.it (via CNAME records) need to be secured as well. The easiest way to do this would be to have these domains secure by Let's Encrypt as well.

I've used Cloudways for years, and decided to continue using the service instead of switching once I found their API docs. I was particularly interested in the Update App Aliases and the Let's Encrypt Install sections.

Unfortunately, both of those endpoints require all domains to be added at once, you can't add or remove domains one at a time. This does however makes sense for generating new SSL certs. I ended up querying user domains that have been added and sending them in the requests to these endpoints. Example below.


For adding domains to the SSL cert, the domain has to be correctly pointed to your app's domain or the request will return a 422 error. What I did to mitigate this was filter the collection of domain names by testing if they are correctly pointed to my main domain (in this case rokk.it).


I noticed that sometimes the SSL updates would not work despite sending the exact same payload with ample time between requests, so I also created a scheduled command that will update the domains and ssl domains every 30 mins in Cloudways. This will hopefully keep things accurate.

The project mentioned in this post is Rokk.it - a service that allow you to advertise during custom URL redirects. It's a great tool for marketing and you can sign up for free!