Skip to content
Advertisement

Laravel main domain routes works for subdomain

When I open test.example.com/main I expect a 404 error because this route is not defined in subdomain routes. but main domain routes works for subdomain. So route Route::get('/main' ... runs and looks valid while it must not be valid.

My routes/web.php :

Route::domain('test.example.com') -> group(function () {
    Route::get('/sub', function(){
        return view('sub.index');
    });
});

Route::get('/main', function(){
    return view('main.index');
})

Advertisement

Answer

i don’t have much experince with laravel, but i think the problem is that main route is begin set to match all domains while /sub route is inside the subdomain group, so i guess you have 2 options

1 – group each domain routes together :

<?php  

Route::domain('test.example.org')->group(function() {

    Route::get('/sub', fn() => view('sub.index'));

});

Route::domain('www.example.org')->group(function() {

    Route::get('/main', fn() => view('main.index'));

});

2 – create a middleware domain to check the host and return 404 error if the host doesn’t match ( i guess the first option would be better )

<?php

Route::get(
  '/main', fn() => view('main.index')
)->middleware('domain');
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement