php – Laravel main domain routes works for subdomain-ThrowExceptions

Exception or error:

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');
})
How to solve:

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');

Leave a Reply

Your email address will not be published. Required fields are marked *