Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel wildcard subdomain per user

Scenario:
Using Laravel 5.2 / Digital Ocean / Laravel Forge (enabled wildcard domains on installing the site)

Every user has its own subdomain.

Main domain: mydomain.com
User domain: {user}.mydomain.com

Added a DNS record (A) - * - ip address to the domain.

Routes:

Route::group(['domain' => '{user}.mydomain.com'], function () {
    return 'This user has a domain.';

    Route::get('user/{id}', function ($account, $id) {
        //
    });
});

Route::get('/', function () {
    return view('pages.home');
});

Problem:
Every user subdomain will skip the group domain and will go into the normal Route get '/'. So it wont return 'This user has a domain.';. What is going wrong?

Laravel Docs Wildcard Subdomains

like image 225
Leguam Avatar asked Oct 21 '25 08:10

Leguam


1 Answers

First, you need to remove return 'This user has a domain.'; as it will prevent this route user/{id} from being registered. Second, try to add a default route to your sub-domains:

Route::group(['domain' => '{user}.mydomain.com'], function () {

    Route::get('/', function ($user) {
        return "This user has a domain: $user";
    });

    Route::get('user/{id}', function ($account, $id) {
        //
    });
});

I hope this will help you.

like image 197
Ismail RBOUH Avatar answered Oct 23 '25 23:10

Ismail RBOUH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!