apachevirtualhostlaravel-5.8debian-jessiephp-7.3

Laravel 5.8 + Apache — Subdomain configuration


So basically my issue is about the routing of a subdomain (or multiple ones) to a single Laravel app. To go more in details I have multiple Route::group and I want to have them "connect" to a specific subdomain. For example:


I have configured my Virtual Hosts like so:

<VirtualHost *:443>
    ServerName domain.co
    DocumentRoot /var/www/domain/public
    DirectoryIndex index.php
    [...]
</VirtualHost>

<VirtualHost *:443>
    ServerName account.domain.co
    DocumentRoot /var/www/domain/public
    DirectoryIndex index.php
    [...]
</VirtualHost>

<VirtualHost *:443>
    ServerName visual.domain.co
    DocumentRoot /var/www/domain/public
    DirectoryIndex index.php
    [...]
</VirtualHost>

As you can see, all the DocumentRoot are the same. In theory with the Laravel configuration explained earlier it should work, but in reality account.domain.co, visual.domain.co and domain.co redirect all to the same application instead of their specific Route::group.


Here is the web.php of my app

Route::get('/', 'IndexController@index')→name('index');
[...]

Route::group([ 'domain' => 'account.domain.com', ],function() {
  Route::get('/', 'AccountController@index')→name('account.index');
  [...]
});

Route::group([ 'domain' => 'visual.domain.com', ],function() {
  Route::get('/', 'VisualController@index')→name('visual.index');
  [...]
});

My configuration is: PHP 7.3, Debian 8 (jessie), Apache 2.4.10 and Laravel 5.8.


Solution

  • You are defining Route::groups without calling them. For this example you probably don't need the groups at all; the documentation is unclear as to that point. I have included them since it will not hurt anything.

    The first two are for the default domain access methods (replaces the first route get('/') which was the only one taken in your code).

    Route::domain('www.domain.com')->group(function() {
    get('/', 'IndexController@index')->name('index');
    })};
    
    Route::domain('domain.com')->group(function() {
    get('/', 'IndexController@index')->name('index');
    })};
    
    Route::domain('account.domain.com')->group(function() {
      Route::get('/', 'AccountController@index')->name('account.index');
    })};
    
    Route::domain('visual.domain.com')->group(function() {
      Route::get('/', 'VisualController@index')->name('visual.index');
    })};
    //...