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

sh1r3f's avatar

Laravel API routes on subdomain are routing on web before they go to api routes

I'm trying to implement laravel api through a subdomain not a prefix. So, I'm working locally using laragon and I already have my project installed on example.test. Then I went to vhosts and added a record to relate api.example.test to 127.0.0.1 as the same domain. Then I Added my RouteServiceProvider to update the api routes and make it use domain ('api.example.test') to redirect routes to my api.php file.

The problem is: All routes to api.example.test are firstly going through web routes then if didn't find any match it goes to api routes and try to match! So how can I fix this?

Screenshots from my code (IDK how to embed them here so I'll put links) My RouteServiceProvider: https://i.stack.imgur.com/IsYAl.png api.php my API routes: https://i.stack.imgur.com/ihBG4.png web.php my Web routes: https://i.stack.imgur.com/Daw60.png The problem! the route checks web routes first before it checks in API!: https://i.stack.imgur.com/MhE4J.png Now I change route in web routes. not the same routes anymore and i check postman again: https://i.stack.imgur.com/4kOtr.png It works fine if not match in web routes!: https://i.stack.imgur.com/TNMf4.png

any solutions would be appreciated!

0 likes
2 replies
cwhite's avatar

You should name your routes so every route has a unique name. For instance, we do something like:

        Route::middleware('api')
            ->domain('api.example.test')
            ->name('api::')
            ->group(base_path('routes/api.php'));

    // later in the routes file

        Route::prefix('software')
            ->name('software.')
            ->group(function () {
                Route::get(
                    'version-report',
                    [SoftwareVersionReportController::class, 'index'],
                )->name('version-report');

and then the route would be

route('api::software.version-report')

Along similar lines, you could use app:: to denote routes in your main web app.

1 like
sh1r3f's avatar

Thank you. I already solved it the problem was with fortify as its routes aren't in web routes file and I guess it was hooking routes to some kind of service provider thus my API routes were going there first. Solved by adding 'domain' => 'example.test' to fortify config file.

Please or to participate in this conversation.