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

vincent15000's avatar

Login and automatic redirection to subdomain

Hello,

I have managed to work with subdomains.

But I don't know how to automatically redirect an authenticated user to his own subdomain and then all routes will depend on his own subdomain.

That means that I have to remember his own subdomain (with a session value ?) and when the session refreshes it has to checking that the subdomain and the authenticated user is effectively associated with his own subdomain.

How can I do that ?

Thanks for your help.

Vincent

0 likes
3 replies
LaryAI's avatar
Level 58

One way to achieve this is to use middleware to check the subdomain and redirect the user if necessary. Here's an example:

  1. Create a middleware class that checks the subdomain and redirects the user if necessary:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectSubdomain
{
    public function handle($request, Closure $next)
    {
        $subdomain = $request->route('subdomain');
        $user = Auth::user();

        if ($user && $user->subdomain !== $subdomain) {
            return redirect()->route('home', ['subdomain' => $user->subdomain]);
        }

        return $next($request);
    }
}

This middleware checks if the authenticated user's subdomain matches the subdomain in the URL. If they don't match, it redirects the user to their own subdomain.

  1. Register the middleware in your Kernel.php file:
protected $routeMiddleware = [
    // ...
    'subdomain' => \App\Http\Middleware\RedirectSubdomain::class,
];
  1. Apply the middleware to your routes:
Route::group(['middleware' => 'subdomain'], function () {
    // your routes here
});

This will ensure that the middleware is applied to all routes in the group.

  1. When a user logs in, store their subdomain in the session:
public function login(Request $request)
{
    // authenticate the user
    // ...

    session(['subdomain' => $user->subdomain]);

    return redirect()->route('home', ['subdomain' => $user->subdomain]);
}

This stores the user's subdomain in the session and redirects them to their own subdomain.

  1. Finally, update your routes to include the subdomain parameter:
Route::group(['domain' => '{subdomain}.example.com'], function () {
    // your routes here
})->where('subdomain', '[a-z0-9]+');

This will capture the subdomain from the URL and pass it to your routes.

With these changes, your application should automatically redirect authenticated users to their own subdomain.

1 like
vincent15000's avatar

@LaryAI Finally with this solution is it really necessary to write the subdomain into the session ?

Then I don't understand how and where I can put the other middlewares.

According to me, this should work, but it doesn't work.

Route::get('/', HomeController::class)->name('home');

Route::middleware(['verified', 'auth', 'subdomain'])->group(function () {
	Route::domain('{subdomain}.bc.org')->group(function () {
		Route::get('dashboard', DashboardController::class)->name('dashboard');
	});
});

I have also tried this and it doesn't work better.

Route::domain('{subdomain}.bc.org')->group(function () {
	Route::middleware(['verified', 'auth'])->group(function () {
		Route::middleware(['subdomain'])->group(function () {
			Route::get('dashboard', DashboardController::class)->name('dashboard');
		});
	});
});

Why doesn't it work ?

Can you help me please ?

Thanks a lot ;).

V

Please or to participate in this conversation.