1

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.

2 Answers2

4

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.

Mohamed Saeed
  • 381
  • 2
  • 7
0

Coming from laravel 8. https://laravel.com/docs/7.x/routing#route-group-subdomain-routing

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

Incase you don't understand what the above quote means as this is basically the issue in this situatuion, it simple implies that you place (register) your subdomain routes right above any root domain route.

What you should NOT do :

//Rooot Domain route(s)
    Route::get('/', function () {
    return view('welcome'); 
});

//Sub Domain route(s)

Route::domain('admin.site')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.site.";
    });
});

What you should DO :


//Sub Domain route(s)

Route::domain('admin.site')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.site.";
    });
});

//Root Domain route(s)
    Route::get('/', function () {
    return view('welcome'); 
});
jovialcore
  • 601
  • 1
  • 5
  • 13