Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 6 app_url set 2 domains

Tags:

laravel

I have some domains that parked on one laravel project, ex:

domain1.com domain2.com

app_url in .env set to domain1.com

When open domain2.com all of files & images open with url domain1.com. I want to when domain2.com open then files & images load in domain2.com.

How I can make it?

like image 831
Dronax Avatar asked Nov 29 '25 20:11

Dronax


2 Answers

You can set app_url dynamic in config

if($_SERVER['HTTP_HOST'] == exmple.com) {
    'url' => env('APP_URL', 'http://localhost'),
}else {
    'url' => env('APP_URL2', 'http://localhost'),
}

in .env file set 2 domain

APP_URL=expample.com
APP_URL2=expample2.com
like image 169
Kamlesh Paul Avatar answered Dec 01 '25 10:12

Kamlesh Paul


In the RouteServiceProvider's boot method add this call $this->setCorrectAppUrl();.

And add this method to your RouteServiceProvider class:

    /**
     * Dynamically set app URL for correct routes building
     */
    protected function setCorrectAppUrl()
    {
        if (isset($_SERVER['HTTP_HOST'])) {
            $host     = $_SERVER['HTTP_HOST'];
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://";
            config(['app.url' => $protocol . $host]);
        }
    }
like image 26
Arm092 Avatar answered Dec 01 '25 11:12

Arm092