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?
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
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]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With