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

samalapsy's avatar

Change Api Route to Subdomain

I'm trying to change my Api route from

http://example.com/api 

to

http://api.example.com

while it at the same time points to the api route in Laravel route. Please how can I achieve that..?

Thanks in Anticipation.

0 likes
3 replies
aurawindsurfing's avatar

app/Providers/RouteServiceProvider

/**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => 'api',
            'namespace' => $this->namespace,
            'prefix' => 'api',
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }

Here you can do your changes.

Hope it helps!

1 like
dzalev's avatar

go to app/Providers/RouteServiceProvider and your mapApiRoutes() function should look like this.

    protected function mapApiRoutes()
    {
        Route::domain('api.example.coml')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
3 likes

Please or to participate in this conversation.