How to route api.example.com to example.com/api so i can just
api.example.com/v1/users
than using
example.com/api/v1/users.
I'm using nginx, thank you.
Ensure these 2 steps are in place.
Check your nginx configuration /etc/nginx/conf.d/example.conf and include the domain in the server_name like so:
server_name example.com api.example.com;
Check that you have a route setup within the routes/api.php file. Using the sub-domain group is optional but be sure that you have the correct routes.
Example of using domain group:
Route::group(['domain' => 'api.example.com'], function () {
Route::get('/v1/users', ['as' => 'api.users.index', 'uses' => 'UserController@index']);
}
Example without use of domain group and allowing for both URL to point to the same Controller (be sure to define its own route names as per the 'as').
Route::get('/v1/users', ['as' => 'api.users.index', 'uses' => 'UserController@index']);
Route::get('/api/v1/users', ['as' => 'users.index', 'uses' => 'UserController@index']);
Update:
Refer to official Laravel 5.3 documentation regarding the use of sub-domain routes https://laravel.com/docs/5.3/routing#route-group-sub-domain-routing
rewrite ^/api/(.*)$ http://api.example.com/$1 redirect;inside your server block for example.com.