phplaravelwildcard-subdomain

laravel main domain route is taking precedence over dynamic subdomain


I am working on a Laravel project and have built a functionality to create dynamic subdomain which is really fine. here is the code of route.

Route::group(['domain' => '{subdomain}.{domain}.{ext}'], function($subdomain)
{ 
    Route::get('/', 'UserController@userPage')->name('userPage');
    Route::post('/', 'UserController@userPageSave')->name('userPageSave');

});

subdomain are working fine but as soon as i try to access the main domain , it takes the precedence over the subdomain

Route::get('/', function() {
    return view('general.homepage');
});

so i put this main route code then i can't access the subdomain anymore. subdomain basically now shows whatever is in the main domain.. its frustrating.


Solution

  • From laravel documentation https://laravel.com/docs/5.8/routing#route-group-sub-domain-routing

    In order to ensure your sub-domain routes are reachable, you should register sub-domain routes before registering root domain routes. This will prevent root domain routes from overwriting sub-domain routes which have the same URI path.