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

jaeyson's avatar

subdomain api with similar path with web.php

hi! i have a route api.domain.tld/image/24 in api.php and domain.tld/image/24 in web.php.

when i access api.domain.tld/image/24 it redirects to web. how can i access the api without redirection?

i have wildcard A host record pointing to the same ip address with the root domain.

// routes/api.php

Route::group(['domain' => '{subdomain}.domain.tld'], function() {
    Route::get('image', function($subdomain) {
        if($subdomain === 'api') {
            Route::get('{id}/{resolution?}', 'API\TestController@test');
        }
    });
});
// routes/web.php

Route::get('image/{id}/{slug?}','TestController@test')->name('image-details');

also, Route::prefix('api') removed in RouteServiceProvider.php

0 likes
2 replies
martinbean's avatar

@jaeyson I don’t really understand your routing. You have Route::get inside another Route::get. That doesn’t really make sense and probably isn’t a valid route.

Just set the domain in your API route group and then place your API routes inside there:

Route::domain('api.domain.tld')->group(function () {
    Route::get('/', function () {
        return response()->json(['message' => 'Hello, world.']);
    });
});
jaeyson's avatar

yes i was doing that exactly. problem is i had similar paths with

  • api.domain.tld/test/3 (API\TestController@apiTest)

  • domain.tld/test/3 (TestController@test)

when i visit api.domain.tld/test/3 it always redirects to domain.tld/test/3. is it because i had similar route path?

Please or to participate in this conversation.