Summer Sale! All accounts are 50% off this week.

tdondich's avatar

Best approach to multiple domains in same project

I have a project that has three "subdomains". One is the main web site, one is a mobile optimized version and one is the administration. Traditionally, I've had each one as a sub-domain. www, mobile, and admin.

What is the best way to approach this in laravel? What if I want to make them their own projects? How would I share the same model (and keep migrations with the model, etc). Or is this possible to do in the same one project?

0 likes
18 replies
pstephan1187's avatar
Level 7

You can group your routes by domain:

Route::group(['domain' => 'www.domain.com'], function(){
    //...
});

Route::group(['domain' => 'mobile.domain.com'], function(){
    //...
});

Route::group(['domain' => 'admin.domain.com'], function(){
    //...
});
6 likes
tdondich's avatar

This would make sense if I used the same domain during development when I do in production. In production, I may be www.example.com but in development, I may reference it as www.example.dev (for my local dev environment). Would using environment variables make sense here?

StuffedGoat's avatar

Maybe this could help you too:

Route::group(array('domain' => Request::server('HTTP_HOST')), function() {  
    
        $value1 = 'mobile';
        $value2 ='admin';
        $url = Request::server('HTTP_HOST');
        $check1 = str_contains($url, $value1);
        $check2 = str_contains($url, $value2);
    
        if($check1 == true){            
            // Routes
        }

        if($check2 == true){            
            // Routes
        }else{
            // Routes
        }
}
bractar's avatar

Hi, I have some issues with the sub-domain routing as well.

So I'm having this:

Route::get('contact', 'ContactController@showForm');
Route::get('about', 'PageController@about');
Route::group(['domain' => 'admin.domain.com'], function()
{
    Route::get('/', 'AdminController@index');
    Route::get('contact', 'AdminController@contact');
});

But when I go to admin.domain.com, the index of the main site is loaded, I'd like to see the admin landing page instead. Also, when I go to admin.domain.com/contact I don't want to see the contact from the main site but the one from the admin instead. When I go to admin.domain.com/about I should get a 404 page. How can I do that? Thanks

bractar's avatar

Hi all, can someone help with the question above? Thank you

bractar's avatar

Hi, this issue came back to me. Can anyone help? Please let me know if you need more details. Thanks

bearcodi's avatar

I know this is old, but the environments example for people stumbling across this:

DOMAIN_WWW=domain.com
DOMAIN_MOBILE=mobile.domain.com

Then access them using Laravel's env() helper function which would also allow you to set lazy fallbacks:

Route::group(['domain' => env('DOMAIN_WWW', 'domain.com')], function(){
    //...
});

Route::group(['domain' => env('DOMAIN_MOBILE', 'mobile.domain.dev')], function(){
    //...
});

Then you can set a .env for all your different environments

4 likes
pkundariya's avatar

Hi Guys,

I had a issue with this kind of hosting setup.

I am facing cache issue many times. Permission denied etc. and my cache folder have 777 permission already.

Is there anyone who can guide me?

thank you parth

infoofajit's avatar

Hello,

Here, you have grouped the sub-domain and its fine. So, can we group multiple top domains not sub-domain the same way so if one Laravel can handle main (top) multiple domain at the same place?

webjobs's avatar

if anyone found a solution let me know as i am looking for a multi-domain multi site to manage all 30 websites in one dashbaord with 3 different templates to choose from

nabinnmg's avatar

@webjobs how did you do it? I have similar project multiple domains each with different color scheme, logo, templates etc but for now all hosted on single server, in later phase if users of anyone of them gets big i will host in different server

KLM113's avatar

Hello everyone, @bearcodi answer seems to be a potential solution, but as it's 5 years old I'd like to ask if there is something better or cleaner I could use nowadays. Thanks in advance.

Rahul15's avatar

I want to host my Laravel project in multiple domain and thiar sub domain too how will it's APP_URL configure ? like abc.com test1.abc.com test2.abc.com xyz.com test1.xyz.com test2.xyz.com then what will APP_URL configurations because of URL() , route(), and assets() how it will function? under stand the web.php but others ??

Snapey's avatar

@Rahul15 wow! thats got to be a record. Hijacking a question from 9 years ago!

Please or to participate in this conversation.