Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iftekhs's avatar
Level 13

How to handle "custom domain" in a multitenant app in laravel forge

Hi, I have a multitenant laravel site that allows our customers to bring in their own custom domain for white labeling. So in our laravel app there is a middleware that detects the tenant based on the domain that is requesting the site content. I instruct my customers to add a "A" record that points to my server's IP address so that my server can load the content based on the domain being accessed. Now the issue is with this structure when a customers custom domain is accessed instead of loading the laravel site that we have it just redirects to the main domain of our laravel site for example if our site is at domain.com and the custom domain is custom.com and we setup a "A" record that points to our server's IP address then upon visiting custom.com it just redirects to domain.com instead of loading the contents of domain.com, your help is appreciated Thank you :)

0 likes
13 replies
JussiMannisto's avatar

Have you configured a virtual host for each of the customer domains?

iftekhs's avatar
Level 13

@JussiMannisto Nope all my customer domains point to a single application and my application level logic takes care of loading the specific content of the specific tenant

JussiMannisto's avatar

@iftekhs That won't work. You have to configure a virtual host for each domain, but they can all point to the same Laravel instance.

If the web server receives a request addressed to customer.domain.com and that isn't found in the virtual host list, how should the server know what to do with it? And how would it know which SSL certificate & private key to use?

The answer is: it can't. It'll forward the request to the first host configured on that port. This is what's happening to you.

2 likes
iftekhs's avatar
Level 13

@JussiMannisto You are correct but after adding the alias it was able to understand which site content to load and about SSL I require all of my customers to use cloudflare with "flexible" SSL mode, this is until I build a system to automatically install SSL on my server for each custom domain. Thank you so much for your response :)

jlrdw's avatar

Is it multitenant or hosting. Just asking because if it's their complete site, it sounds like you are just hosting it for them.

Sorry that sounded a little confusing to me.

iftekhs's avatar
iftekhs
OP
Best Answer
Level 13

I have added the custom domain as alias of my root domain from forge and it worked correctly

iftekhs's avatar
Level 13

@JussiMannisto yes because I require all of my clients to use cloudflare with "flexible" SSL mode because did not built a system to programatically install SSL for all custom domains yet

1 like
akazad91's avatar

@iftekhs did get any Solution? i am getting same issue. how can I solve it, I don't know.

colinlongworth's avatar

@akazad91 I did this about two weeks ago. It's pretty involved and assumes you are using Cloudflare or a similar DNS service if you want clients to be able to bring their domain.

On the forge side, you need nginx to accept any domain e.g.

server_name ~^(www.)?([a-z0-9-]+)(..+)?$;

In Cloudflare, you need to use Custom Hostnames (https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/)

Therefore, the high level process would be:

  1. You set up the client subdomain on your system e.g. client-a.my-saas.com
  2. The client adds their subdomain e.g. crm.the-client.com and you then add that to YOUR Cloudflare account as a custom hostname (I've automated this process via the Cloudflare API)
  3. The client then adds a CNAME to their DNS, which would point to client-a.my-saas.com

When set up, the client therefore has HTTPs to your system but via their own custom domain.

MikeWhileCoding's avatar

@colinlongworth Wouldn't it also be possible (if using Laravel Forge) to implement the SDK/API into my application so the user automaticly creates a site alias (https://forge.laravel.com/api-documentation#add-site-aliases) and after that obtain a ssl certificate for their subdomain with simple A record (https://forge.laravel.com/api-documentation#ssl-certificates).

This may not be a better solution than cloudflare, but it does not vendor lock you to using cloudflare

dkroft's avatar

Inject a middleware (or a route group around most of your existing routes) for detecting the presence of a subdomain in the route url

  • map {subdomain:slug} -> if a matching client subdomain exists, apply your multi-tenant constraints
  • if no matching client subdomain -> it's your regular "unbranded" traffic

Please or to participate in this conversation.