简体   繁体   中英

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:

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>

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>

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM