Multiple Domains for Languages

Published 1 year ago by Refringe

So let's say I have two domains; hello.com and bonjour.com. I need these two domains to share the same codebase. The only difference, other than the domain, is the localization language; one is en and the other fr. What would be the best way to dynamically set the app's URL and locale settings based on the current request's URL?

Right now I have Homestead configured so both domains are loading the same codebase. I'm fairly new to Laravel... just curious if anyone else has had a set-up like this?

I was even thinking about something as simple as this. Because the .env file would have the en domain as the default you would only need to overwrite the defaults if the fr domain was found in the URL.

if ($_SERVER['HTTP_HOST'] === 'bonjour.com') {
    config([
        'app.url' => 'http://bonjour.com',
        'app.locale' => 'fr'
    ]);
}

I just don't really know where the "proper" location for this logic to run would be.

bobbybouwmann

Why not create some custom .env variables which are application specific? The .env file is not in version control and is used for application specific settings like database credentials and so on. A perfect fit for your case ;)

// .env

APP_URI=http://bonjour.com
APP_LOCALE=fr

You can then do something like this in your config

// config/app.php

'url' => env('APP_URI', 'http://localhost'),

'locale' => env('APP_LOCAL', 'en'),
bobbybouwmann

Also if you even would at a third site in Dutch for example you would have to update the if statement since there are now three cases instead of two. With the .env solution you can add as many sites as want, since it doesn't depend on any code.

Refringe

Using the .env file had crossed my mind but I don't know how to load different versions of the .env file (one per domain) while at the same time pointing each of the domains to the same source code. Is there a technique for doing that?

Refringe

Or would you set each of the domains to load their own copy of the code, but connect to the same database? Is there any downside to that, other than having to manage making releases to two directories at the same time? Am I missing something?

Please sign in or create an account to participate in this conversation.