Michał Lipa Michał Lipa - 6 months ago 137
PHP Question

Laravel subdomain route - NotFoundHttpException

I'm working on API in my Laravel project, and have problem with setting up subdomain (Ubuntu).

I set up

Virtualhost
,
Routing
, enabled
vhost_alias


VirtualHost:

<VirtualHost *:80>
ServerName domain.io
ServerAlias domain.io
DocumentRoot mypath

<Directory mypath>
AllowOverride All
Order allow,deny
Allow from all
</Directory>

</VirtualHost>
<VirtualHost *:80>
ServerName api.domain.io
ServerAlias api.domain.io
DocumentRoot mypath

<Directory mypath>



AllowOverride All
Order allow,deny
Allow from all
</Directory>

</VirtualHost>


My route:

Route::group(['domain' => 'api'.env('APP_HOST')], function() {

Route::get('test', function() {
return 'test';
});

});


Url
http://api.domain.io/
redirect to main domain
domain.io
. When I visit my route
http://api.happs.io/test
I get error (like there would not be that route):


NotFoundHttpException in RouteCollection.php line 161:

Answer

Your hosts file is correct. The issue is your env('APP_HOST') is a static value but it needs to be dynamic with your setup.

An option, and likely the easiest approach (it's the one I use too), is to just create a route for the sub domain manually.

Route::group(['domain' => 'api.happs'], function() {
    // Do something
});
Comments