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

t0berius's avatar

laravel multi domain setup (different domains, NO subdomains)

What's the most efficient way to use a laravel application with many different domains?

I would like to have the correct domain displayed in the address bar, not only a redirect of the domains to one "main" domain.

Is there a way using laravel directly?

Other option would be to setup the webserver with some filter rules and use a "main" domain only "internal", nginx can rewrite all session data / URLs too, but I'm looking for a way using laravel.

0 likes
4 replies
LaryAI's avatar
Level 58

One way to handle multiple domains in Laravel is to use the Route::group method to define routes for each domain. Here's an example:

Route::group(['domain' => 'example.com'], function () {
    // Define routes for example.com
});

Route::group(['domain' => 'example.net'], function () {
    // Define routes for example.net
});

Route::group(['domain' => 'example.org'], function () {
    // Define routes for example.org
});

You can also use middleware to handle domain-specific logic. For example, you could create a middleware that sets a variable based on the current domain, and then use that variable in your controllers or views. Here's an example:

namespace App\Http\Middleware;

use Closure;

class SetDomainVariable
{
    public function handle($request, Closure $next)
    {
        $domain = $request->getHost();
        if ($domain === 'example.com') {
            $request->attributes->set('domain', 'example.com');
        } elseif ($domain === 'example.net') {
            $request->attributes->set('domain', 'example.net');
        } elseif ($domain === 'example.org') {
            $request->attributes->set('domain', 'example.org');
        }
        return $next($request);
    }
}

Then, you can apply this middleware to your routes or controllers:

Route::group(['middleware' => 'setDomainVariable'], function () {
    Route::get('/', function () {
        $domain = request()->attributes->get('domain');
        return view('welcome', compact('domain'));
    });
});

This will set the $domain variable based on the current domain, which you can then use in your views.

t0berius's avatar

I'm asking mainly, because of the use of SESSION_DOMAIN because it needs to be set for each domain?

Please or to participate in this conversation.