Reputation: 978
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:
Upvotes: 1
Views: 973
Reputation: 5608
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
});
Upvotes: 0
Reputation: 1458
Try this, put the domain and subdomain in the NameServer:
<VirtualHost *:80>
ServerName domain.io api.domain.io
DocumentRoot mypath
<Directory mypath>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Upvotes: 0
Reputation: 3530
Add *.
before your domain name on ServerAlias
and make sure you have a wildcard CNAME.
<VirtualHost *:80>
ServerName domain.io
ServerAlias *.domain.io
DocumentRoot mypath
<Directory mypath>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Upvotes: 0