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

RuzHan's avatar

How to implement Laravel API on subdomain?

I understand APIs and know how to make a simple one in laravel, what I am struggling with is how to make an api that is accessible on api.example.com instead of example.com/api/v1/.

I have searched on Google, laracasts and the docs but I am still scattered. I don't know how to go about this. I want example.com to just be a vue frontend and api example.com to be laravel backend, I need help on understanding how to setup my webserver/cpanel to work as such or what would you do to make a website like this. Any help will be greatly appreciated.

0 likes
8 replies
bobbybouwmann's avatar

Well, you have two options here most of the time.

Either you deploy your application two times, one on the main domain, and one on a subdomain with the API prefix. In that case, you need to point the subdomain to the correct server in your DNS.

The second option is using one application. For this option, you need to point the subdomain to the same IP as your normal application. After that, you need to add the domain prefix route group to your routes, to make that work.

Documentation: https://laravel.com/docs/8.x/routing#route-group-subdomain-routing

3 likes
laracoft's avatar

@roshanjafri

Route::group(['domain' => 'https://api.example.com/'], function () {
    Route::get('api/v1/', [controller::class, 'method']);
})
3 likes
RuzHan's avatar

Thank you for your comment, the second option seems more suitable.

Could this be done using aliases? for example the api.example.com would be an alias of example.com and in the filemanager on my cPanel, root/laravel_project/routes/web.php I will use the Subdomain routing to check if 'api' was the subdomain and treat the requests on api.example.com as API but they are actually being redirected to example.com, sort of fooling the client into thinking they are accessing a separate subdomain?

Would this be considered good practice?

RuzHan's avatar

Hey, thanks for your comment. I have already read the docs and implemented this in my web.php but what I am struggling with is how to setup the project and settings in my cPanel as such that when someone visits api.example.com the routes will be handled by web.php in my laravel_project/routes/ and NOT api.example.com subdomain folder which is its own website with a cgi_bin folder and index.html.

automica's avatar

If you are using aliases then you don’t need to maintain 2 copies of your filesystem. Remove your subdomain folder and modify the subdomain to point to your main file system.

in CPanel, in Subdomains, you can click on the pencil icon to change 'Document Root'

laracoft's avatar

@roshanjafri set the root folder of api.example.com to the same public folder of your Laravel project then add the route prefix which I showed above.

secrethash's avatar

I know this is an old thread but maybe this may help someone in future.

The Correct way to do this is probably by utilizing the App\Providers\RouteServiceProvider. There you will find it being setup in the boot method. Here is how I utilize it by also using my config/app.php. If config is set then it uses the domain() else uses the default prefix() method.

<?php
namespace App\Providers;

...
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
...

class RouteServiceProvider extends ServiceProvider
{

    ...

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            if (!config('app.api'))
            {
                Route::prefix('api')
                    ->middleware('api')
                    ->namespace($this->namespace)
                    ->group(base_path('routes/api.php'));
            }
            else
            {
                Route::domain(config('app.api'))
                    ->middleware('api')
                    ->namespace($this->namespace)
                    ->group(base_path('routes/api.php'));

            }

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

    ...

}

and here is how I do it in my config/app.php:

<?php

return [

    ...

    /*
    |--------------------------------------------------------------------------
    | Application URL
    |--------------------------------------------------------------------------
    |
    | This URL is used by the console to properly generate URLs when using
    | the Artisan command line tool. You should set this to the root of
    | your application so that it is used when running Artisan tasks.
    |
    */

    'url' => env('APP_URL', 'http://localhost'),

    'api' => env('API_URL', false),

    ...

];

5 likes

Please or to participate in this conversation.