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

MichaelDaly's avatar

Handling multi-tenant sub and custom domains

I am developing a website builder application which has multiple points of entry with respect to domains.

When a user signs up, they enter a sub-domain which is used to access their publicly available website.

Also, once the user is in the admin panel, they have the option to add their own custom domain, and again, this is used to access their publicly available website.

Essentially, it is a multi-tenant application with sub-domain and main domain access.

I am wanting to ask how other people are handling this type of scenario as my routes config has to cater for many scenarios and it is becoming difficult to manage all of the points of entry.

Here is a summary of the situation I have to cater for:

Marketing site

  • company.com

Customer Website

  • mysite.company.com
  • mysite.com

Admin Panel

  • company.com/admin
  • mysite.company.com/admin
  • mysite.com/admin

My intentions were to try to make things easier for my clients, but this seems to have complicated the route logic.

I am wondering whether I should only allow my customers to access the admin by using their sub-domain, and locking down all other access.

But I am not sure if this is even a problem and whether I am overthinking things and actually this is ok and what other people are doing.

It would be interesting to hear how other people are handling this type of set up.

0 likes
5 replies
martinbean's avatar
Level 80

@mike What services like Heroku do is allow you to create a CNAME that maps to the sub-domain version of your application’s URL.

I think in this scenario, you’ll need to do some routing logic to map a custom domain name a customer’s added to their actual subdomain, and then group your routes using the sub-domain:

// Marketing site routes
Route::group(['domain' => 'example.com'], function () {
    // Marketing home page
    Route::get('/', 'HomeController');
});

// Customer routes
Route::group([
    'domain' => '{account}.example.com',
    'middleware' => ['any', 'middleware', 'here'],
    'namespace' => 'Account',
], function () {
    // Customer home page route
    // All routes in this group will receive $account as first parameter
    // Use route–model binding to have $account be an Account instance
    Route::get('/', 'HomeController');
});

Whatever approach you take, make sure to cache you’re domain-to-sub-domain look-ups. You don’t want to be performing the same database query on each and every HTTP request. Cache the mappings, and then re-cache them when a domain is added/removed.

1 like
MichaelDaly's avatar

Hi @martinbean

Thanks for the quick reply. I have implemented a very similar approach so it seems like I was heading down the right path.

I thought I might have been making things more complicated than they had to be, but it seems like a logical way approach it.

Good tip with the caching too! Don't want to be querying that information on every request!.

Thanks again

viedev's avatar

I've created an example Laravel repo that gives some real examples of how to handle custom domains in Laravel, if anyone comes across this post in their search to figure out custom domains.

Snapey's avatar

@viedev 6 years old man. Are you going to spam all questions about domains or SSL ?

Please or to participate in this conversation.