Ad

How To Exclude Www From Dynamically Created Subdomain

- 1 answer

In my Laravel Application after User registration , I Assign a subdomain to each user based on her username.

If a User want to access a illegal and unregistered subdomain , I show 404 error page.

for that this is my Routes :

Route::group(['domain' => Config::get('app.url')], function () {
     //some Other Routes for all Members
});


// Match a subdomain of my domain
Route::group(['domain' => '{username}.' . Config::get('app.url')], function () {
    Route::get('/', ['uses' => '[email protected]']);
    Route::get('/news/{news_alias}', ['uses' => '[email protected]_details']);
});

and in config/app.php, I have url option like this:

'url'             => 'tc.dev'

All things works fine But my Problem is when user try to open www.tc.dev domain , laravel shows 404 error page because laravel thinks that as a username and could not found it and show error page.

How can I exclude www from subdomains so that act as a normal domain.

Ad

Answer

I haven't used Laravel, but I think this would do the trick.

$routerFunction = function() {
    //some Other Routes for all Members
};

Route::group(['domain' => Config::get('app.url')], $routerFunction);
Route::group(['domain' => 'www.' . Config::get('app.url')], $routerFunction);


// Match a subdomain of my domain
Route::group(['domain' => '{username}.' . Config::get('app.url')], function () {
    Route::get('/', ['uses' => '[email protected]']);
    Route::get('/news/{news_alias}', ['uses' => '[email protected]_details']);
});
Ad
source: stackoverflow.com
Ad